我试图找出使用JPQL从SQL中的连接表中检索数据的最佳方法。这是场景:
我有一个用户实体,它有一个费用清单列表。 ExpenseList实体映射到User,它是OneToMany关系。 当我运行程序并为用户创建新列表时,User_Lists(JoinedTable)表使用用户的id和它映射到的列表ID进行更新。现在的问题是尝试从User_Lists表中检索数据。每次我尝试它都说它没有映射。
用户类:
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类:
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;
}
}
我之前尝试的数据访问方法:
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;
}
答案 0 :(得分:2)
您已经拥有了所需的一切,并且不需要额外的专栏。您正在使用对象关系映射器,因此处理对象而不是表。
用户实例已经有一个关联的ExpenseList集合,因此您只需导航该关联:
User user = entityManager.find(User.class, id);
List<ExpenseList> expenses = user.getExpenseList();
如果您真的想查询,可以将关系设为双向,以便ExpenseList通过@ManyToOne对其用户进行反向引用。然后你可以简单地做:
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();
答案 1 :(得分:0)
我找到了解决方案。我可以添加一个列,其中包含创建List的User的ID,而不是Joining Tables。然后以这种方式检索数据。这么容易多了!