2017-07-27 54 views
1

我想獲得按日期降序排序的數據,但始終我與ASC得到它, 我嘗試2種方式,但沒有好的結果:順序按日期字段在Spring數據JPA不工作

我有這個資源庫:

@Repository 
public interface CollectRepository extends JpaRepository<Collect, Long> 

1的方式,我以前在@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); 

,並呼籲使用功能:

@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 JPA的數據版本:1.10.5

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 

收款人實體。 RELEASE

這是一個錯誤還是我的代碼有問題? 我該如何解決這個問題?

+0

加收實體 –

+0

的我加入了實體@MaciejKowalski – Spartan

+0

哪裏是你的用戶id的代碼? – xenteros

回答

1

下面的方法將返回的Collect列表與datestartendpayee之間id爲payeeId。一旦你添加Payee到我可以調整到模型的問題。

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

你的功能無法正常工作的通用實體 – Spartan

+0

@Spartan你有一個列表或一組?設置無序 – xenteros

+0

確定我有一個列表 – Spartan

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); 

,它工作正常:)

+0

我已經嘗試過日,但第二個將無法工作,因爲我必須遵循對實體字段的名字不在數據庫中 – Spartan

+0

@Spartan請參閱我的答案更新,你可能是錯的:第二個。 – ngc4151

+0

您使用可分頁這是另一種方式,但對於我而言,我不使用網頁呢,和你的情況,你像在租賃實體 – Spartan