【发布时间】:2018-01-02 23:54:20
【问题描述】:
我正在尝试使用 DESC 获取按日期排序的数据,但我总是使用 ASC 获取它, 我尝试了两种方法,但都没有好的结果:
我有这个仓库:
@Repository
public interface CollectRepository extends JpaRepository<Collect, Long>
第一种方式,我在@query 中使用了排序:
@Query("SELECT c FROM Collect c LEFT JOIN c.payee p WHERE p.userId=:userId AND c.date BETWEEN :startDate AND :endDate ORDER BY c.date DESC")
List<Collect> getCollectionHistory(@Param("userId") String userId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
第二种方式,我使用了排序
@Query("SELECT c FROM Collect c LEFT JOIN c.payee p WHERE p.userId=:userId AND c.date BETWEEN :startDate AND :endDate")
List<Collect> getCollectionHistory(@Param("userId") String userId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate, Sort sort);
并使用 :
调用函数collectionList = collectRepository.getCollectionHistoryByCollector(userId, startDate, endDate, new Sort(Direction.DESC, "date"));
收集实体
@Entity
@Table(name = "collect")
@SequenceGenerator(name = "SEQ", sequenceName = "collect_id_seq", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "collect_id", nullable = false))
public class Collect extends GenericEntity {
@Column(name = "collector_user_id")
private String collectorUserId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "payer")
private Payer payer;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "payee")
private Payee payee;
@Column(name = "amount", precision = 5)
private BigDecimal amount;
@Column(name = "date", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@JsonSerialize(using = IsoDateSerializer.class)
private Date date;
@Column(name = "reason")
private String reason;
@Column(name = "reference")
private String reference;
// getters and setters
收款人实体:
@Entity
@Table(name = "payee")
@SequenceGenerator(name = "SEQ", sequenceName = "payee_id_seq", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "payee_id", nullable = false))
public class Payee extends GenericEntity {
private String userId;
@OneToMany(mappedBy = "payee", cascade = CascadeType.ALL)
private List<Collect> collects;
@OneToMany(mappedBy = "payee", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<PayeePayer> payeePayers;
我正在使用 Spring Data JPA 版本:1.10.5.RELEASE
这是一个错误还是我的代码有问题? 我该如何解决这个问题?
【问题讨论】:
-
添加收集实体的代码
-
我添加了实体@MaciejKowalski
-
你的用户 ID 在哪里?
-
@Spartan 添加收款人实体以获得完美答案
-
跑题了,为什么用户ID还是字符串?
标签: java spring jpa spring-data-jpa hql