JPA map relation entity parentID
Question
could someone help me to understand how can I define an entity with JPA mapping that has a relation with it self?
For example, my entity is CompanyDivision, divisionA contains divisionB, divisionC and divisionB contains divisionB1, divisionB2
-
divisionA
- divisionB
- divisionB1
- divisionB2
- divisionC
- divisionB
Thank you!
Solution
It's not different from a relation between 2 different Entities. Here's an example:
class CompanyDivision {
@OneToMany(mappedBy = "parentDivision")
private Set<CompanyDivision> childDivisions = new HashSet<CompanyDivision>();
@ManyToOne
@JoinColumn(name = "FK_PARENT_DIVISION")
private CompanyDivision parentDivision;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow