How to check a collection size in JPA2

后端 未结 1 962
粉色の甜心
粉色の甜心 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 criteriaQuery = cb.createQuery(Book.class);
    Root book = criteriaQuery.from(Book.class);
    Predicate predicate = cb.ge(cb.size(book.get(Book_.authors)), 2);
    criteriaQuery.where(predicate);
    criteriaQuery.select(book);
    

    0 讨论(0)
提交回复
热议问题