本文介绍了JPA映射关系实体parentID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我了解如何通过JPA映射定义一个与自身有关系的实体吗?
could someone help me to understand how can I define an entity with JPA mapping that has a relation with it self?
例如,我的实体是CompanyDivision,部门A包含部门B,部门C而DivisionB包含DivisionB1,DivisionB2
For example, my entity is CompanyDivision, divisionA contains divisionB, divisionCand divisionB contains divisionB1, divisionB2
- 部门
- 部门B
- divisionB1
- divisionB2
- divisionA
- divisionB
- divisionB1
- divisionB2
谢谢!
推荐答案
与2个不同实体之间的关系没有什么不同.这是一个示例:
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; }
这篇关于JPA映射关系实体parentID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- divisionB
- 部门B