2017-06-19 66 views
0

我想获取所有子级用户。 和我有两列家长和孩子休眠查询获取子女

在父母

table--

id pname pid(fk of parent) 

1 A 
2 B  1 
3 C  2 
在子表

---

id cname cid(fk of child) pid(fk of parent) 

1 AA      1 
2 BA  1     2 
3 BC  2     2 
4 CA  3     3 
5 CC  4     3 

如果父 - 让所有的孩子

if parent B-- get BA,BB,CA,CC

如果父母C-- 获得CA,CC

我VL得到答案,但它不是单一的查询.. thers任何冬眠单查询要做到这一点

更新.......... ....

public class Parent implements PersistEntity{ 

    @Id 
    @Column(name = "parent_id") 
    @SequenceGenerator(name="OH",sequenceName="OH", allocationSize=1) 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="OH") 
    private Long parentid; 

    @Column(name = "parent_name") 
    private String parentName; 

    @ManyToOne 
    @JoinColumn(name = "fk_parent_id") 
    private Parent fkparentId; 
} 

public class Child implements PersistEntity{ 

    @Id 
    @Column(name = "child_id") 
    @SequenceGenerator(name="OP",sequenceName="OP",allocationSize=1) 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="OP") 
    private Long childId; 

    @Column(name = "child_name") 
    private String childName; 

    @ManyToOne 
    @JoinColumn(name = "fk_child_id") 
    private Child fkchildId; 

    @ManyToOne 
    @JoinColumn(name = "fk_office_hierarchy_parent") 
    private Parent fkparentId; 
} 
+0

目前还不清楚你问什么用递归查询。但是这仍然看起来像一个问题,基本上要求写入你的规范的代码。我们不是免费的代码写作服务。 –

+0

使用Hibernate时,您正在编写和编写针对类模型的查询,而不是表。这是拥有ORM的原因之一。 –

回答

1

我解决了JPA