我正在嘗試使用WorkoutCaseIdfrom從兩個不同的表返回數據ReportedWorkout,并使用PaymentDatefrom對它們進行排序Payment。報告鍛煉+-------------------+----------------+| ReportedWorkoutId | WorkoutCaseId |+-------------------+----------------+支付+-----------+--------------------+--------------+---------------+| PaymentId | ReportedWorkoutId | PaymentDate | PaymentAmount |+-----------+--------------------+--------------+---------------+我想返回數據為:SELECT * FROM ReportedWorkoutJOIN table2 ON ReportedWorkout.ReportedWorkoutId = Payment.ReportedWorkoutIdWHERE WorkoutCaseId = '123'ORDER BY PaymentDate DESC@Entity@Table(name = "ReportedWorkout")public class ReportedWorkoutEntity extends BaseEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ReportedWorkoutId") private Long reportedWorkoutId; @Column(name = "WorkoutCaseId") private String workoutCaseId; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ReportedWorkout") private Set<PaymentEntity> payments = new LinkedHashSet<>();...}@Entity@Table(name = "Payment")public class PaymentEntity extends BaseEntity{ @Id @Column(name = "PaymentId" , nullable = false) @GeneratedValue(strategy = GenerationType.IDENTITY) private long paymentId; @ManyToOne @JoinColumn(name = "ReportedWorkoutId") private ReportedWorkoutEntity reportedWorkout; @Column(name = "PaymentAmount") private BigDecimal paymentAmount; @Column(name = "PaymentDate" , nullable = false) private LocalDate paymentDate;...}我讓它通過以下方式返回數據WorkoutCaseId:@Repositorypublic interface ReportedWorkoutRepository extends CrudRepository<ReportedWorkoutEntity, Long> { ReportedWorkoutEntity findByWorkoutCaseId(String workoutCaseId);}但我不知道如何通過 PaymentDate 訂購?findByWorkoutCaseIdOrderByPaymentDateDesc 我收到以下錯誤:Caused by: org.springframework.data.mapping.PropertyReferenceException: No property paymentDate found for type ReportedWorkoutEntity!
2 回答

慕哥6287543
TA貢獻1831條經驗 獲得超10個贊
由于 paymentDate 不是 ReportedWorkoutEntity 的屬性,因此該錯誤是有道理的。對于存儲庫方法,請使用相對于 ReportedWorkoutEntity 的 paymentDate 的完全限定名稱。
所以:findByWorkoutCaseIdOrderBy Payments PaymentDateDesc

森欄
TA貢獻1810條經驗 獲得超5個贊
當您想使用另一個實體時,您必須在 @Repository 接口中定義的方法簽名中提及它。
正如@churd 所提到的,您必須僅使用該格式創建方法簽名。
還有另一個選項,如方法頂部提到的@Query,并在其中定義要執行的sql。
添加回答
舉報
0/150
提交
取消