【发布时间】:2015-04-15 03:28:59
【问题描述】:
假设我在表组织中有父子信息,如下所示:
id name parent_id
1 A 1
2 A1 1
3 A2 1
4 A11 2
使用 Oracle,我可以使用“开始于/连接方式”获取组织的所有后代或祖先。比如下面的sql会获取“A”下的所有子树都包含自己(即A、A1、A2、A11)
select * from Organization start with id=1 connect by nocycle prior id=parent_id;
或者这个sql会得到A11的所有祖先,包括它自己(即A11,A1,A)
with o_hier as (select o.id, o.parent_id, CONNECT_BY_ISCYCLE as lvl from Organization o start with id=4 connect by nocycle prior parent_id = id) select o.* from Organization o, o_hier where o.id = o_hier.id union all select o.* from Organization o, o_hier where o.id = o_hier.parent_id and o_hier.lvl = 1;
现在我想像这样将该表映射到 OrganizationEntity:
@Entity
@Table(name = "Organization")
public class OrganizationEntity {
//getter/setter omitted for readability
@Id
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@???
List<OrganizationEntity> descendants = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
@???
List<OrganizationEntity> ancestors= new ArrayList<>();
}
我知道可能存在性能问题,但我们可以使用 Hibernate/JPA 映射类似的东西吗?
【问题讨论】:
标签: oracle hibernate jpa one-to-many hierarchical