【问题标题】:Retrieve Data From Joined Table with JPQL使用 JPQL 从连接表中检索数据
【发布时间】:2016-12-17 02:08:40
【问题描述】:

我正在尝试找出使用 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;

    }

【问题讨论】:

    标签: java spring hibernate jpa jpql


    【解决方案1】:

    您已经拥有所需的一切,不需要额外的列。您正在使用 Object 关系映射器,因此请处理对象而不是表。

    User 实例已经有一个关联的 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();
    

    【讨论】:

    • 这比我的解决方案简单得多,我什至没有意识到您可以通过这种方式检索集合。谢谢大佬
    • 为什么你把正确的答案改成了一个重复这个只在 2 天后发布的答案?
    【解决方案2】:

    我想出了一个解决方案。而不是加入表,我可以只添加一个带有创建列表的用户 ID 的列。然后以这种方式检索数据。轻松多了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多