Use Spring Data JPA to Find By Column from 1 table and order by column from another table

Angelina :

I am trying to return data from 2 different tables using WorkoutCaseId from ReportedWorkout and sort them using PaymentDate from Payment.

ReportedWorkout

+-------------------+----------------+
| ReportedWorkoutId | WorkoutCaseId  |
+-------------------+----------------+

Payment

+-----------+--------------------+--------------+---------------+
| PaymentId | ReportedWorkoutId  | PaymentDate  | PaymentAmount |
+-----------+--------------------+--------------+---------------+

I want to return data as:

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;
...
}

I got it to return data by WorkoutCaseId:

@Repository
public interface ReportedWorkoutRepository extends CrudRepository<ReportedWorkoutEntity, Long> {    
    ReportedWorkoutEntity findByWorkoutCaseId(String workoutCaseId);
}

But I don't know how to order it by PaymentDate?

findByWorkoutCaseIdOrderByPaymentDateDesc 

I get following error:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property paymentDate found for type ReportedWorkoutEntity!
camtastic :

Since paymentDate is not a property of ReportedWorkoutEntity the error makes sense. For the repository method use the fully qualified name for paymentDate relative to ReportedWorkoutEntity.

So: findByWorkoutCaseIdOrderByPaymentsPaymentDateDesc

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164326&siteId=1