尝试将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的责任。
发布于 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即可
https://stackoverflow.com/questions/52704499
复制相似问题