Crud Repository,日期比较

时间:2017-07-25 11:53:13

标签: java date spring-boot

我有以下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'

我想在扩展CrudRepository的界面内创建此查询。方法是

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

但是这个方法总是返回一个空列表,我认为是由于Java date和SQL TimeStamp?如果您想了解更多细节,我可以将它们添加到问题中。我的班级:

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

和洗涤班

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

我的可翻页

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

我传递给findWashHistoryByTimeBetweenOrderByTimeDesc(new LimitAndOffsetPageable (0,100))(偏移= 0,限制= 100)

1 个答案:

答案 0 :(得分:0)

您的问题是由使用错误的命名约定引起的:您的实体应如下所示:(删除冗余行 - 如果您想添加非空的约束,请将其添加到@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;
}

现在,方法可能是:

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

排序属性应放入pageable对象。