简体   繁体   English

原始存储库,日期比较

[英]Crud Repository, date comparison

I have the following sql query: 我有以下SQL查询:

select * from wash_history w where w.wash_id in
  (select wash.id from wash where wash.car_wash_id = 17)
and w.time between '2017-07-13' and  '2017-07-22'

And I want to create this query inside interface that extends CrudRepository . 我想在扩展CrudRepository接口内创建此查询。 The method is 方法是

Page<WashHistory> findWashHistoryByTimeBetweenOrderByTimeDesc(Date dateFrom,
                                                              Date dateTo,
                                                              Pageable pageable);

But this method always returns an empty list, I think is it due to Java date and SQL TimeStamp? 但是此方法始终返回一个空列表,我认为是由于Java日期和SQL TimeStamp导致的吗? If you want some more details I can add them to question. 如果您需要更多详细信息,可以将其添加到问题中。 My class: 我的课:

public class WashHistory implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "wash_price")
    private Double washPrice;
    @Size(max = 2147483647)
    @Column(name = "car_wash_name")
    private String carWashName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date time;
    @Size(max = 2147483647)
    @Column(name = "status")
    private String status;
    @Column(name = "bonuses")
    private Integer bonuses=0;
    @JoinColumn(name = "wash_id", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Wash washId;}

ANd wash class 洗类

 public class Wash implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 2147483647)
    @Column(name = "status")
    private String status;
    @Column(name = "using_bonuses")
    private Boolean usingBonuses=false;
    @Basic(optional = false)
    @NotNull
    @Column(name = "last_status_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastStatusTime;
    @OneToMany(mappedBy = "washId", fetch = FetchType.LAZY)
    private Set<WashHistory> washHistorySet;
    @OneToOne(mappedBy = "washId", fetch = FetchType.LAZY)
    private WashComment washCommentSet;
    @JoinColumn(name = "car_wash_id", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private CarWash carWashId;}

My pageble 我的pageble

public class LimitAndOffsetPageable extends PageRequest {
        private int page;
        private int size;
        public LimitAndOffsetPageable(int page, int size){
            super(page,size);
            this.page=page;
            this.size=size;
        }
        @Override
        public int getOffset(){
            return this.page;
        }
}

I pass to findWashHistoryByTimeBetweenOrderByTimeDesc(new LimitAndOffsetPageable (0,100)) (offset = 0 , limit = 100) 我传递给findWashHistoryByTimeBetweenOrderByTimeDesc(new LimitAndOffsetPageable (0,100)) (offset = 0,limit = 100)

Your problem is caused by using bad naming conventions: Your entities should look like following: (removed redundant lines - if you want to add constraints like not null, add them in @Column ) 您的问题是由使用错误的命名约定引起的:您的实体应如下所示:(已删除的多余行-如果要添加约束(例如不为null),请在@Column添加它们)

@Entity
public class WashHistory implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "wash_price")
    private Double washPrice;
    @Column(name = "car_wash_name")
    private String carWashName;
    @Column(name = "time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date time;
    private String status;
    private Integer bonuses=0;
    @JoinColumn(name = "wash_id", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Wash wash;
}

Now, the method could be: 现在,方法可以是:

Page<WashHistory> findAllByWashIdIdAndTimeBetween(Integer id, Date start, Date end, Pageable pageable)

Sort property should be put into pageable object. 排序属性应放在pageable对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM