Retrieve Data From Joined Table with JPQL

836 Views Asked by At

I'm trying to figure out what the best way to retrieve data from a Joined Table in SQL using JPQL. Here's the scenario:

I have a User entity, which has a List of Expense Lists. The ExpenseList entity is mapped to the User, it is a OneToMany relationship. When I run the program and create a new list for the User, The User_Lists (JoinedTable) table updates with the User's id and the list's id that it is mapped to. The problem now is trying to retrieve the data from the User_Lists table. Every time I try it says it is not mapped.

User Class:

package com.emmaobo.expensetracker.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.emmaobo.expensetracker.enumeration.AccountType;

@Entity
@Table(name="USERS")
public class User 
{

    @Id @GeneratedValue
    private Long id;

    @Column(name="USERNAME")
    private String username;

    @Column(name="PASSWORD")
    private String password;

    @Column(name="EMAIL")
    private String email;

    @Enumerated(EnumType.STRING)
    private AccountType account;

    @OneToMany(targetEntity=ExpenseList.class)
    @JoinTable(name = "USER_LISTS")
    private List<ExpenseList> list;


    public User(){}

    public User(String username, String password, String email)
    {
        this.username = username;
        this.password = password;
        this.email = email;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public AccountType getAccount() {
        return account;
    }

    public void setAccount(AccountType account) {
        this.account = account;
    }

    public List<ExpenseList> getList() {
        return list;
    }

    public void setList(List<ExpenseList> list) {
        this.list = list;
    }

    public void addList(ExpenseList newList)
    {
        this.list.add(newList);
    }

}

ExpenseList Class:

package com.emmaobo.expensetracker.model;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="EXPENSE_LIST")
public class ExpenseList 
{
    @Id @GeneratedValue
    private Long id;

    private String title;

    @Column(name = "TOTAL")
    private BigDecimal total;

    @OneToMany(targetEntity = Item.class)
    private List<Item> items;

    public ExpenseList(){}

    public ExpenseList(String title)
    {
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Data Access method I was previously trying:

public List<ExpenseList> viewUsersLists(Long id)
{
    //TODO fix this (study how to retrieve data from Joined Tables)
    em = emf.createEntityManager();
    et = em.getTransaction();
    List<ExpenseList>usersLists = new ArrayList<ExpenseList>();

    @SuppressWarnings("unchecked")
    TypedQuery<Long> query = (TypedQuery<Long>) em.createQuery("SELECT LIST_ID FROM USERS_EXPENSE_LIST Where USER_ID = "+id);
    List<Long> usersListIDs = query.getResultList();

    for(Long expid : usersListIDs)
    {
        usersLists.add((ExpenseList)em.createQuery("SELECT id FROM ExpenseList Where id ="+expid, ExpenseList.class));
    }

    return usersLists;

    }
2

There are 2 best solutions below

2
On

You have everything you need already and don't need an additional column. You are using an Object Relational Mapper so deal with objects and not tables.

A User instance already has an associated collection of ExpenseList so you can simply navigate that association:

User user = entityManager.find(User.class, id);
List<ExpenseList> expenses = user.getExpenseList();

If you really wanted to query you can make the relationship bi-directional so that ExpenseList has a back-reference to it's User via a @ManyToOne. Then you can simply do:

User user = entityManager.find(User.class, id);
TypedQuery<ExpenseList> q = 
       em.createQuery("SELECT e FROM UserExpenseList e Where e.user = :user);
q.setParameter("user", user);
List<ExpenseList> expenses  q.getResultList();
0
On

I figured out a solution. Instead of Joining Tables I can just add a column with the ID of the User that created the List. And then retrieve the data that way. So much easier!