简体   繁体   中英

Fetching child entities in Mysql/Hibernate

I use JPA as persistence engine to handle database queries. The tables (MySQL) are as follows:

Parent - table
+-----+---------+
|id   |   name  |
+-----+---------+
| 1   |  name1  |
| 2   |  name2  |
| 3   |  name3  |
+---------------+

child1 - table 
+--+---------+------+
|id|parent_id|name  |
+--+---------+------+
| 1|   1     |  qwe |
| 2|   1     |  asd | 
| 3|   1     |  dsf |
| 4|   2     |  xzc |
+--+---------+------+

child2 - table 
+--+---------+------+------+
|id|parent_id|type  |name  |
+--+---------+------+------+
| 1|   1     |  1   | qqq  |
| 2|   1     |  1   | www  |
| 3|   1     |  1   | eee  |
| 4|   1     |  2   | aaa  |
| 5|   1     |  2   | sss  |
| 6|   1     |  2   | ddd  |
| 7|   1     |  3   | zzz  |
| 8|   2     |  1   | xxx  |
+--+---------+------+------+

And my classes are

@Entity
@Table(name = "parent")
public class Parent{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private int id;

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

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
    private List<Child1> children1;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
    private List<Child2> children2;

}

@Entity
@Table(name = "child1")
public class Child1{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private int id;

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", nullable = false)
    private Parent parent;

}

@Entity
@Table(name = "child2")
public class Child2{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private int id;

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

    @Column(name = "type")
    private int type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", nullable = false)
    private Parent parent;

}

I would like to get Parents and child entities (embedded their own List objects in Parent java object) in the following manner. I just want to get child1 entities with maximum id for each parent.

(for the given example, child1(id:3) for parent(id:1) and child1(id:4) for parent(id:2)).

Also I want to get child2 with maximum id for each type of child2.

(for the given example, child2(id:3)(max of type:1), child2(id:6)(max of type:2), child2(id:7)(max of type:3) for parent(id:1) and child2(id:8)(max of type:1) for parent(id:2))

At last, Parent(id:1) has one Child1(id:3) in List children1 and has three Child2(id:3, id:6, id:7) in List children2.

Sorry for my poor English.

Thanks for any help.

You should really post what you have tried so far, but since I can't Comment Everywhere™ yet, I will help you with the first one and you can try the second on your own.

I am assuming you are asking for the HQL to perform your query. Try this:

select max(c.id), c.parent.id from Child1 c group by child1.parent.id

The second query you wanted is very similar, if you can get that one to work then it should be easy.

I think you want to filter on the child collection. Look into Hibernate Filters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM