JPA map relation entity parentID

断了今生、忘了曾经 提交于 2019-12-11 19:43:13

问题


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

Thank you!


回答1:


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;
}


来源:https://stackoverflow.com/questions/2743895/jpa-map-relation-entity-parentid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!