我有一对多的关系:
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);
这里有什么问题?
答案 0 :(得分:3)
您似乎忘了在注释属性中添加mappedBy
:
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
private List<Transaction>transactions = new ArrayList<>();