[英]One-to-Many query for collection
I have a One-to-Many relation: 我有一对多的关系:
public class Account{
@OneToMany(cascade=CascadeType.ALL)
private List<Transaction>transactions = new ArrayList<>();
}
public class Transaction{
@ManyToOne
@JoinColumn(name="ACCOUNT_ID")
private Account account;
}
I want to get all Accounts
for a User
, and it works, but Transaction
list is empty. 我希望获得
User
所有Accounts
,并且它可以工作,但是Transaction
列表是空的。 Is this a matter of entity mapping or should I modify my query? 这是实体映射的问题还是应该修改我的查询?
I started with (empty transaction list): 我开始(空交易清单):
TypedQuery<Account>query = em.createQuery("SELECT a FROM Account a WHERE a.user.id = ?1",Account.class);
also tried to join like so (no accounts returned at all): 也试图像这样加入(根本没有账号):
TypedQuery<Account>query = em.createQuery("SELECT a FROM Account a JOIN a.transactions t WHERE a.user.id = ?1",Account.class);
What's wrong here? 这有什么不对?
It seems you forgot to add mappedBy
in the annotation properties: 您似乎忘了在注释属性中添加
mappedBy
:
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
private List<Transaction>transactions = new ArrayList<>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.