Search code examples
hibernatespring-bootcriteria

Hibernate query to get child


i want to fetch all child users. and i have two columns parent and child

in parent table--

id  pname  pid(fk of parent)

1   A
2   B      1
3   C      2

in child table---

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 A -- get all child

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

if parent C-- get CA,CC

i vl get the answer but its not single query.. thers any hibernate single query to do this

update..............

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

Solution

  • i solved using Recursive query in jpa