criteriaBuilder在列表中搜索

criteriaBuilder在列表中搜索

本文介绍了Spring jpa criteriaBuilder在列表中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table(name = book)
public class Book扩展内容{

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

现在,这是我的 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));
}
};
}

如何检查给定的 authorId

Error:

  java .lang.IllegalStateException:非法尝试在org.hibernate.jpa.criteria.path.Abs​​tractPathImpl.illegalDereference(AbstractPathImpl.java:98)〜[hibernate-entitymanager- 4.3.11.Final.jar:4.3.11.Final] 
在org.hibernate.jpa.criteria.path.Abs​​tractPathImpl.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]
pre

解决方案

为此,您可以使用Join with谓词:

请参阅贝洛w代码,

  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);
}
};
}


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]
解决方案

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);
        }
    };
}

这篇关于Spring jpa criteriaBuilder在列表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:45