我有一个1米关系的父子实体,从父到子是不定向的。我希望所有的孩子都能通过排序和分页来了解父ID。 我使用Spring Data尝试了以下方法: - 使用@Query创建对实体的查询,但排序仅适用于Parent实体 - 尝试了本机查询,但这不支持分页和排序 - 尝试使用PagingAndSortingRepository,但它只有一个findAll(),它将带来所有父记录,而不仅仅是一个
你对Spring Data这样做有什么想法,还是我应该尝试一些像Hibernate的Criteria这样的东西?
这是我尝试过的(Leg是父实体,Cashflows是子实体):
@Repository
public interface CashflowRepository extends JpaRepository<Cashflow, Long> {
@Query(value = "SELECT cf FROM Leg leg JOIN leg.cashflows cf WHERE
leg.id=:legId",
countQuery = "SELECT count(cf) FROM Leg leg JOIN leg.cashflows cf
WHERE leg.id=:legId")
Page<Cashflow> findByLeg(@Param("legId") Long legId, Pageable pageable);
}
我将存储库称为:
cashflowRepository.findByLeg(1234, new PageRequest(1, 20, new Sort(
new Sort.Order(Sort.Direction.ASC, "paymentDate"))));
问题是排序属性"paymentDate"
是应用于父实体(Leg
)而不是应用于子(Cashflows
)。
See the exception: org.hibernate.QueryException: could not resolve property: paymentDate of: com.db.treasury.tpmt.model.Leg [SELECT cf FROM com.db.treasury.tpmt.model.Leg leg JOIN leg.cashflows cf WHERE leg.id=:legId order by leg.paymentDate desc]