这是我的repository界面:
public interface ContentRepository extends JpaRepository<Content, Long> {
@Query(value = "select c from Content c where c.ContentCategory.genre = :genre and c.ContentType.genre = :contentType")
Iterable<Content> findByTypeAndCategory(@Param("contentType") String contentType, @Param("genre") String genre);
}
Run Code Online (Sandbox Code Playgroud)
这是ContentPOJO:
@Entity
@Table(name = "content")
public class Content implements Serializable {
public Content() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
private ContentCategory contentCategory;
@ManyToOne
private ContentType contentType;
// other methods }
Run Code Online (Sandbox Code Playgroud)
在这里我的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" …Run Code Online (Sandbox Code Playgroud) 我有一个包含作者列表的书类:
@Entity
@Table(name = "book")
public class Book extends Content {
@ManyToMany(fetch = FetchType.LAZY)
private List<Author> authors;
...}
Run Code Online (Sandbox Code Playgroud)
现在,这是我的BookSpecifications班级:
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));
}
};
}
Run Code Online (Sandbox Code Playgroud)
如何检查authorId列表中的给定内容?
错误:
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]
Run Code Online (Sandbox Code Playgroud) 我有两个简单的桌子content,contentType
@Entity
@Table(name = "content")
public class Content implements Serializable {
public Content() {}
public Content(String title, String description) {
this.title = title;
this.description = description;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@ManyToOne
private ContentCategory contentCategory;
@ManyToOne
private ContentType contentType;
// getter/setters
}
@Entity
@Table(name = "contentType")
public class ContentType implements Serializable {
public ContentType() {}
public ContentType(String contentType) {
this.contentType = contentType;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@NotNull
private String contentType; …Run Code Online (Sandbox Code Playgroud)