猿问

使用 Spring Data JPA 从一个表中按列查找并从另一个表中按列排序

我正在尝试使用WorkoutCaseIdfrom从两个不同的表返回数据ReportedWorkout,并使用PaymentDatefrom对它们进行排序Payment。


报告锻炼


+-------------------+----------------+

| ReportedWorkoutId | WorkoutCaseId  |

+-------------------+----------------+

支付


+-----------+--------------------+--------------+---------------+

| PaymentId | ReportedWorkoutId  | PaymentDate  | PaymentAmount |

+-----------+--------------------+--------------+---------------+

我想返回数据为:


SELECT * FROM ReportedWorkout

JOIN table2 ON ReportedWorkout.ReportedWorkoutId = Payment.ReportedWorkoutId

WHERE 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:


@Repository

public 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!


拉丁的传说
浏览 113回答 2
2回答

慕哥6287543

由于 paymentDate 不是 ReportedWorkoutEntity 的属性,因此该错误是有道理的。对于存储库方法,请使用相对于 ReportedWorkoutEntity 的 paymentDate 的完全限定名称。所以:findByWorkoutCaseIdOrderBy&nbsp;Payments&nbsp;PaymentDateDesc

森栏

当您想使用另一个实体时,您必须在 @Repository 接口中定义的方法签名中提及它。正如@churd 所提到的,您必须仅使用该格式创建方法签名。还有另一个选项,如方法顶部提到的@Query,并在其中定义要执行的sql。
随时随地看视频慕课网APP

相关分类

Java
我要回答