1

I have following entity relationship:

@Entity
public class Parent {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Long id;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    private List<Child> children;
}

and Child entity:

@Entity
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Long id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Parent parent;
}

I use Spring JPA repositories, so for calling data I use default method parentRepository.findOne(id)

I need fetch List of Parent entity where each entity of Parent will be fetched limited size of Children, for example 10.

Can you tell me if it possible with JQPL or HQL and what this select looks like? Thanks in advance.

EDIT:

Not I tried use hibernate annotation @BatchSize like this:

@BatchSize(size = 5)
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Child> children;

But didn't work, when I fetch Parent all Children are fetched.

Joe Grizz
  • 113
  • 2
  • 11
  • 1
    Possible duplicate of https://stackoverflow.com/questions/25073122/how-to-limit-a-collection-in-an-objects-property-in-hibernate – Madhusudana Reddy Sunnapu Mar 25 '18 at 13:06
  • @MadhusudanaReddySunnapu Hi, I'm sorry I wrong formulated my question. Now I edit it, and you can see why this question isn't solution for me. I cannot do reverse select for Children where id of Parent is 'X', because I also need select List of Parents, with their limited number of Children. – Joe Grizz Mar 25 '18 at 13:33
  • I doubt if it is possible either. Go through https://stackoverflow.com/questions/26328187/is-it-possible-to-limit-the-size-of-a-onetomany-collection-with-hibernate-or-jp – Madhusudana Reddy Sunnapu Mar 25 '18 at 15:42

1 Answers1

0

Use this way

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@Size(min=1, max=3)
private List<Child> children;
dasunse
  • 2,839
  • 1
  • 14
  • 32