Java 按日期排序字段在Spring Data JPA中不起作用
我试图用DESC按日期排序数据,但我总是用ASC排序, 我尝试了两种方法,但没有好结果: 我有一个存储库:Java 按日期排序字段在Spring Data JPA中不起作用,java,spring,jpa,spring-data-jpa,hql,Java,Spring,Jpa,Spring Data Jpa,Hql,我试图用DESC按日期排序数据,但我总是用ASC排序, 我尝试了两种方法,但没有好结果: 我有一个存储库: @Repository public interface CollectRepository extends JpaRepository<Collect, Long> 收集实体 @Entity @Table(name = "collect") @SequenceGenerator(name = "SEQ", sequenceName = "collect_id_seq", a
@Repository
public interface CollectRepository extends JpaRepository<Collect, Long>
收集实体
@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;
@实体
@表(name=“收款人”)
@SequenceGenerator(name=“SEQ”,sequenceName=“收款人id\u SEQ”,allocationSize=1)
@AttributeOverride(name=“id”,column=@column(name=“paye\u id”,nullable=false))
公共类受款人扩展通用实体{
私有字符串用户标识;
@OneToMany(mappedBy=“payee”,cascade=CascadeType.ALL)
私人名单收集;
@OneToMany(mappedBy=“payee”,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
私人名单付款人;
我使用的是SpringDataJPA版本:1.10.5.0
这是一个bug还是我的代码有问题?
如何解决此问题?以下方法将返回
收款
列表,其中日期
介于开始
和结束
之间,以及收款人
id收款人id
。一旦将收款人
添加到问题中,我可以根据模型进行调整
List<Collect> findAllByPayeeUserIdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);
列出FindAllByPayeUserAndDateBetweeNorderByDateDesc(字符串PayeUserId、日期开始、日期结束);
您能试试这个吗:
List<Collect> findByPayeeUserIdAndDateBetweenOrderByDateDesc(String payeeUserId, Date start, Date end);
列出FindBayeeUserAndDateBetweeNorderByDateDesc(字符串PayeUserId、日期开始、日期结束);
或
List findByPayeeUser_id和datebetween norderbydatedesc(字符串payeuserid,日期开始,日期结束);
对于第二个,我有一个类似的项目存储库,如下所示:
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);
}
import com.buhane.property.domain.entity.Lease;
导入org.springframework.data.domain.Page;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.repository.paging和sortingrepository;
导入org.springframework.stereotype.Repository;
导入java.util.List;
@存储库
公共接口租赁存储库扩展了分页和排序存储库{
页面findbyaapplicationid(长applicationId,可分页页面);
页面FindByaApplicationProperty_Id(长属性Id,可分页页面);
列出FindByaApplicationAndActiveTrue(长applicationId);
}
请注意线路
Page<Lease> findByApplicationProperty_Id(Long propertyId, Pageable page);
Page findbyaapplicationproperty\u Id(长属性Id,可分页页面);
它工作正常:)添加Collect entity的代码我添加了entity@Maciejkowalski你的用户ID在哪里?@Spartan添加收款人entity以获得完美答案脱离主题但为什么用户ID是字符串?你的函数不起作用,我已经在其他服务中尝试过这种方法,但它不起作用also@Spartan你有一张单子还是一套et无序确保我有一个列表,这就是为什么我问它是否是框架中的一个bug,或者如果需要,不迁移到新版本,没有异常,没有错误,所有数据都用ASC排序,我认为它是按rows@Does它通过userId和date-between正确地限制了行数?我已经尝试了第一个,但是第二个不起作用,因为e我必须遵循实体上的字段名称,而不是database@Spartan请查看我的更新答案,您可能对第二个答案有错误。您使用的是Pageable,这是另一种方式,但对于我的情况,我还没有使用pages,对于您的情况,您在租约中这样命名Property_Identity@Spartan不,实际上是我的租约ty有一个对应用程序实体的引用,该实体引用的属性实体是“PropertyId”而不是“Property\u Id”。而我的属性实体有一个PK列,它只是“Id”。
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);