Limit @Onetomany List

Asked

Viewed 110 times

0

Good morning Guys! I have some problems in a list that in some cases do not need to return it completely

@OneToMany(mappedBy="local", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@OrderBy("dataContagem DESC")
private List<Contagem> contagens;

I have tried using the @Size and @Max validation. I am using Hibernate with the JPA specification. It is possible to perform this limit of the Onetomany list in the JPQL query ?

  • Not possible. Use Native Query or JPQL.

  • Ideally you remove the EAGER of @OneToMany and control the number of results manually, with a separate query just to pick up Count, so you make your code much more flexible. The EAGER is a "code Smell", avoid using it, prefer to use the LAZY.

1 answer

0

I found this answer that should suit your need in stackoverflow-en

@OneToMany(mappedBy="local", cascade=CascadeType.ALL)
@OrderBy("dataContagem DESC")
@Fetch(FetchMode.SELECT)
@Size(min=1, max=10)
private List<Contagem> contagens;

Remove the fetch EAGER pq it can cause overhead if it is used with a list and has many records, look for calling the contagens.size(); before sending the object, the active proxy will make the selection of the list of contagens

Browser other questions tagged

You are not signed in. Login or sign up in order to post.