Reputation: 265
I have two entities:
@Entity
@Table(name="Account")
public class AccountEntity{
@Id
private Id;
}
@Entity
@Table(name="AccountMap")
public class AccountMapEntity {
@Id
private Long Id;
private Long parentId;
private Long childId;
}
How can I get the child Accounts by the parent Id? I would write a query for example
select acc from AccountEntity acc join AccountMapEntity map on acc.id = map.childId
where map.parentId = :parentId;
How can I do this with @OneToMany/@ManyToOne annotations, so I wouldn't have to write a join in my query?
*Edited I edited the query to make it obvious that I only want records from AccountEntity.
Upvotes: 0
Views: 423
Reputation: 1914
Seems very simple too me
@Entity
@Table(name="AccountMap")
public class AccountMapEntity {
@Id
private Long Id;
@ManyToOne
@JoinColumn(name="parentId")
private AccountEntity parent;
@ManyToOne
@JoinColumn(name="childId")
private AccountEntity child;
}
Upvotes: 1