一对多查询集合

问题描述 投票:1回答:1

我有一对多的关系:

public class Account{
  @OneToMany(cascade=CascadeType.ALL)
  private List<Transaction>transactions = new ArrayList<>();
}

public class Transaction{
    @ManyToOne
    @JoinColumn(name="ACCOUNT_ID")
    private Account account;
}

我想得到所有AccountsUser,它的工作原理,但Transaction列表是空的。这是实体映射的问题还是应该修改我的查询?

我开始(空交易清单):

TypedQuery<Account>query  = em.createQuery("SELECT a FROM Account a WHERE a.user.id = ?1",Account.class);

也试图像这样加入(根本没有账号):

 TypedQuery<Account>query  = em.createQuery("SELECT a FROM Account a JOIN a.transactions t WHERE a.user.id = ?1",Account.class);

这有什么不对?

java hibernate jpa orm
1个回答
3
投票

您似乎忘了在注释属性中添加mappedBy

@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
private List<Transaction>transactions = new ArrayList<>();
© www.soinside.com 2019 - 2024. All rights reserved.