Spring Data JPA:如何使用带有@Query 注释的对象?
Spring Data JPA: How to use objects with the @Query annotation?
考虑以下 JpaRepository
。
interface ExpectedLossRepository : JpaRepository<ExpectedLoss, Long> {
fun getByDealId(dealId: Long): ExpectedLoss
@Query("select el from ExpectedLoss el where el.dealId = ?1.dealId and ...")
fun getByExtendedTerm(@Param("extended_term") term: ExtendedTerm): ExpectedLoss
}
第一个函数让我可以通过 dealId
查询 ExpectedLoss
个实体,目前一切顺利。但是现在我需要构建一个复杂的查询,因此不想传递所有必需的原语,而是传递一个对象的实例。
此查询无效。如何引用传递的对象?
@Query("select el from ExpectedLoss el where el.dealId = (?1).dealId and ...")
我同时想通了。正确的语法是:
interface ExpectedLossRepository : JpaRepository<ExpectedLoss, Long> {
@Query("select el from ExpectedLoss el where el.dealId = :#{#term.dealId}")
fun getByExtendedTerm(@Param("extended_term") term: ExtendedTerm): ExpectedLoss
fun getByDealId(dealId: Long): ExpectedLoss
}
考虑以下 JpaRepository
。
interface ExpectedLossRepository : JpaRepository<ExpectedLoss, Long> {
fun getByDealId(dealId: Long): ExpectedLoss
@Query("select el from ExpectedLoss el where el.dealId = ?1.dealId and ...")
fun getByExtendedTerm(@Param("extended_term") term: ExtendedTerm): ExpectedLoss
}
第一个函数让我可以通过 dealId
查询 ExpectedLoss
个实体,目前一切顺利。但是现在我需要构建一个复杂的查询,因此不想传递所有必需的原语,而是传递一个对象的实例。
此查询无效。如何引用传递的对象?
@Query("select el from ExpectedLoss el where el.dealId = (?1).dealId and ...")
我同时想通了。正确的语法是:
interface ExpectedLossRepository : JpaRepository<ExpectedLoss, Long> {
@Query("select el from ExpectedLoss el where el.dealId = :#{#term.dealId}")
fun getByExtendedTerm(@Param("extended_term") term: ExtendedTerm): ExpectedLoss
fun getByDealId(dealId: Long): ExpectedLoss
}