Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring jpa criteriaBuilder search in a list

I have a book class with a list of authors:

@Entity
@Table(name = "book")
public class Book extends Content {

    @ManyToMany(fetch = FetchType.LAZY)
    private List<Author> authors;
...}

Now, this is my BookSpecifications class:

public static Specification<Book> authorIdIs(Long authorId) {
    return new Specification<Book>() {
        @Override
        public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
            return cb.isTrue(root.get("authors").get("id").in(authorId));
        }
    };
}

How can check the given authorId is in the list?

Error:

java.lang.IllegalStateException: Illegal attempt to dereference path source [null.authors] of basic type
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at com.tarameshgroup.derakht.service.specs.BookSpecifications$3.toPredicate(BookSpecifications.java:40) ~[classes/:na]
    at org.springframework.data.jpa.domain.Specifications$ComposedSpecification.toPredicate(Specifications.java:189) ~[spring-data-jpa-1.9.2.RELEASE.jar:na]
like image 494
CVV Avatar asked Jan 18 '16 11:01

CVV


People also ask

What is CriteriaBuilder in JPA?

CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings. See JavaDoc Reference Page... interface serves as the main factory of criteria queries and criteria query elements. It can be obtained either by the EntityManagerFactory. persistence.

How use like criteria query?

On the Create tab, click Query Design. Click Add and the Customers table gets added to the query designer. Double-click the Last Name and City fields to add them to the query design grid.

How do you add multiple or conditions to Spring data?

Please refer the following code : Criteria criteria = new Criteria(); criteria. andOperator(Criteria. where("siteCode").is(siteCode)); if(paymentMode !=

What is the use of JpaSpecificationExecutor?

The JpaSpecificationExecutor interface adds methods which will allow us to execute Specification s, for example, these: List<T> findAll(Specification<T> spec); Page<T> findAll(Specification<T> spec, Pageable pageable); List<T> findAll(Specification<T> spec, Sort sort);


1 Answers

For that you can use Join with predicate:

Refer below code,

public static Specification<Book> authorIdIs(Long authorId) {
    return new Specification<Book>() {
        @Override
        public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Join join = root.join("authors");                   
                return cb.equal(join.get("id"),authorId);
        }
    };
}
like image 167
Harshal Patil Avatar answered Oct 06 '22 22:10

Harshal Patil