【问题标题】:Order By a date field not working in Spring Data JPA按日期字段排序在 Spring Data JPA 中不起作用
【发布时间】: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


【解决方案1】:

以下方法将返回startendpayee 之间的Collectdate 列表,ID 为payeeId。将Payee 添加到问题后,我可以根据模型进行调整。

List<Collect> findAllByPayeeUserIdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);

【讨论】:

  • 你的功能不起作用,我已经在其他服务中尝试过这种方式,它也不起作用
  • @Spartan 你有列表还是集合?集合是无序的
  • 确定我有一个列表
  • 这就是为什么我要问它是否是框架中的一个错误,或者如果需要不迁移到较新的版本,没有例外没有错误,所有数据都用 ASC 排序,我认为它是按 ID 排序的行
  • @它是否正确地通过 userId 和 date 限制行?
【解决方案2】:

你可以试试这个:

List<Collect> findByPayeeUserIdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);

List<Collect> findByPayeeUser_IdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);

对于第二个,我的一个项目有一个类似的存储库,它是这样的:

import com.buhane.property.domain.entity.Lease;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LeaseRepository extends PagingAndSortingRepository<Lease, Long> {
    Page<Lease> findByApplicationId(Long applicationId, Pageable page);

    Page<Lease> findByApplicationProperty_Id(Long propertyId, Pageable page);

    List<Lease> findByApplicationIdAndActiveTrue(Long applicationId);
}

请注意这一行

Page<Lease> findByApplicationProperty_Id(Long propertyId, Pageable page);

它工作正常:)

【讨论】:

  • 我已经尝试了第一个,但是第二个不行,因为我必须遵循不在数据库中的实体上的字段名称
  • @Spartan 请查看我的更新答案,您可能对第二个答案有误。
  • 您正在使用 Pageable 这是另一种方式,但就我而言,我还没有使用页面,而对于您的情况,您正在像 Lease 实体中那样命名 Property_Id
  • @Spartan 不,实际上我的 Lease 实体引用了 Application 实体,该实体引用了带有“PropertyId”而不是“Property_Id”的 Property 实体。我的 Property 实体有一个 PK 列,它只是“Id”。
猜你喜欢
  • 2021-11-29
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2019-08-07
  • 2013-11-13
  • 2018-07-05
  • 1970-01-01
  • 2017-09-21
相关资源
最近更新 更多