首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Data --支持自定义查询的PagingAndSortingRepository?

Spring Data --支持自定义查询的PagingAndSortingRepository?
EN

Stack Overflow用户
提问于 2018-10-08 22:29:17
回答 1查看 1.2K关注 0票数 1

尝试将PagingAndSortingRepository与自定义查询混合,但没有成功..

自定义回购:

public interface SiteRepositoryCustom
{
    public List<SitesDbRecord> getActiveSites();
}

Impl repo:

@Repository
public class SiteRepositoryImpl implements SiteRepositoryCustom
{
    private static final Logger logger = ...

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<SitesDbRecord> getActiveSites()
    {
        logger.info( "getActiveSites start" );

       try
       {
          String hql = "select s from SitesDbRecord s where s.isActive = true";

          return em.createQuery( hql ).setMaxResults( Integer.MAX_VALUE ).getResultList();
       }
       catch ( Exception e )
       {
          logger.error( "getActiveSites failed.", e );

          return null;
       }
   }
}

注入到服务中的repo:

public interface SiteRepository extends PagingAndSortingRepository<SitesDbRecord, Integer>, SiteRepositoryCustom  {
    public List<SitesDbRecord> getActiveSites( Pageable pageable );
    public List<SitesDbRecord> getActiveSites();
}

如果我只是扩展CrudRepository (没有Pageable方法),那么一切都是正常的。尝试扩展PagingAndSortingRepository (使用或不使用分页方法)时,Spring无法启动

PropertyReferenceException: No property getActiveSites found for type SitesDbRecord!

在自定义查询中使用PagingAndSortingRepository的正确方法是什么?可能弄错了,但我认为提供分页/排序处理是Spring的责任。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-08 22:56:34

如果SitesDbRecord具有名为active的布尔属性,则应为:

public interface SiteRepository extends PagingAndSortingRepository<SitesDbRecord, Integer> {
    public List<SitesDbRecord> findByActiveIsTrue( Pageable pageable );
    public List<SitesDbRecord> findByActiveIsTrue();
}

无需扩展您的自定义存储库,只需实现PagingAndSortingRepository即可

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52704499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档