2017-06-19 110 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