订单按日期字段在Spring Data JPA中不起作用

时间:2017-07-27 10:07:08

标签: java spring jpa spring-data-jpa hql

我正在尝试使用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

这是一个错误,还是我的代码中有错误? 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

以下方法将在Collectdate以及start之间返回end的{​​{1}}列表,其中包含payee。将payeeId添加到问题后,我可以根据模型进行调整。

Payee

答案 1 :(得分: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);

并且工作正常:)