How to check a collection size in JPA2

后端 未结 1 965
粉色の甜心
粉色の甜心 2021-01-05 08:54

Consider the following:

@Entity
public class Book 
{ 
    private List authors;
    @ElementCollection
    public List getAuthors         


        
相关标签:
1条回答
  • 2021-01-05 09:27

    In JPQL:

    select b from Book where size(b.authors) >= 2
    

    Using the criteria API (but why would you replace such a simple static query with the following mess?):

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> criteriaQuery = cb.createQuery(Book.class);
    Root<Book> book = criteriaQuery.from(Book.class);
    Predicate predicate = cb.ge(cb.size(book.get(Book_.authors)), 2);
    criteriaQuery.where(predicate);
    criteriaQuery.select(book);
    
    0 讨论(0)
提交回复
热议问题