Spring Data JPA中按日期字段排序不起作用

3

我正在尝试按日期倒序排序数据,但总是按升序排列,我尝试了两种方法,但没有好的结果:

我有这个存储库:

@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
这是一个错误还是我在代码中有些问题? 我该如何解决这个问题?

@Spartan 为完美答案添加收款人实体。 - xenteros
1
离题了,但是为什么用户ID要用字符串呢? - ngc4151
@ngc4151 因为我正在使用外部用户管理,所以我正在使用 Auth0,并将用户 ID 生成为字符串。 - Spartan
你在从数据库获取列表后修改它吗? - Abdullah Khan
@AshishLohia 所有实体都扩展了一个包含生成的 Id 列的通用实体。 - Spartan
显示剩余7条评论
2个回答

3
下面的方法将返回一个Collect列表,其中datestartend之间且payee的ID为payeeId。一旦您添加了Payee到问题中,我可以调整模型。
List<Collect> findAllByPayeeUserIdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);

你的函数不起作用,我已经在其他服务中尝试过这种方式,也不起作用。 - Spartan
@Spartan 你有一个列表或集合吗?集合是无序的。 - xenteros
当然,我有一个列表。 - Spartan
这就是为什么我在询问是否是框架中的错误,以便在需要时迁移到新版本,没有任何异常或错误,所有数据都按升序排序,我认为它是按行ID排序的。 - Spartan
@它是否正确地通过userId和日期之间限制行数? - xenteros
显示剩余2条评论

0

你能试试这个吗:

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
@Spartan请查看我的更新答案,你可能对第二个答案有误。 - ngc4151
你正在使用Pageable,这是另一种方式,但对于我的情况,我还没有使用页面,而对于你的情况,你在Lease实体中命名Property_Id。 - Spartan
@Spartan 不,实际上我的租赁实体引用了一个应用程序实体,该实体具有对“PropertyId”而不是“Property_Id”的属性实体的引用。而我的属性实体具有一个主键列,只是“Id”。 - ngc4151

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接