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. 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:
@OneToMany(cascade=CascadeType.ALL, mappedBy="account")
private List<Transaction>transactions = new ArrayList<>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.