JPQL / HQL - Limit items for list of children

一世执手 提交于 2019-12-23 03:19:08

问题


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.


回答1:


Use this way

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


来源:https://stackoverflow.com/questions/49475975/jpql-hql-limit-items-for-list-of-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!