我有一对多的关系:
public class Account{
@OneToMany(cascade=CascadeType.ALL)
private List<Transaction>transactions = new ArrayList<>();
}
public class Transaction{
@ManyToOne
@JoinColumn(name="ACCOUNT_ID")
private Account account;
}
我想得到所有Accounts
为User
,它的工作原理,但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);
这有什么不对?
您似乎忘了在注释属性中添加mappedBy
:
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
private List<Transaction>transactions = new ArrayList<>();