使用Pageable查询集合

时间:2014-04-08 12:00:21

标签: spring-data spring-data-jpa

我有两个实体。具有一对多关系的NewsCategoryNewsItem

新闻分类

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

NewsItem

@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属性的一部分?

0 个答案:

没有答案