我有两个实体。具有一对多关系的NewsCategory
和NewsItem
。
@Entity
public class NewsCategory extends AbstractEntity<Long> {
private String name;
@OneToMany(cascade = CascadeType.ALL)
private List<NewsItem> items = new ArrayList<>();
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<NewsItem> getItems() {
return items;
}
}
@Entity
public class NewsItem extends AbstractEntity<Long> {
private String title;
private LocalDate startDate;
private LocalDate endDate;
private String resource;
@Column(columnDefinition = "text")
private String content;
// getters and setters...
}
我希望items
集合可以分页,但我在为它定义存储库接口方面遇到了一些困难。
此界面无法正常工作。
public interface NewsCategoryRepository extends JpaRepository<NewsCategory, Long> {
@Query("SELECT e.items FROM #{#entityName} e WHERE e = ?1")
public List<NewsItem> findItems(NewsCategory category, Pageable pageable);
}
执行findItems()
时,会抛出以下异常。
Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [newscatego0_.id.items] with element property reference [startDate] [SELECT e.items FROM NewsCategory e WHERE e = ?1 order by e.items.startDate asc]
如何修改上述界面,以便使用Spring Data和items
返回Pageable
属性的一部分?