假设我有以下实体:
@Entity
@Table(name="ORDER_ITEM")
public class Order {
@Id
@Column(name = "ORDER_ID")
private Long id;
@Column(name = "ORDER_VALUE")
private BigDecimal value;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
@Column(name = "CREATED_AT", columnDefinition = "date")
private Date createdAt;
@Column(name = "HOURS_MAX")
private Integer hoursMax = 24;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Integer getHoursMax() {
return hoursMax;
}
public void setHoursMax(Integer hoursMax) {
this.hoursMax = hoursMax;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", value=" + value +
", createdAt=" + createdAt +
", hoursMax=" + hoursMax +
'}';
}
}
因此,我需要在 hoursMax 整数字段的周期最大值中通过 createAt 日期字段对订单限制进行条件查询。我看到很多使用 Date 实例 java 减去整数文字值(以小时为单位)的示例,如下例所示:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Order> cq = cb.createQuery(Order.class);
Root<Order> orderRoot = cq.from(Order.class);
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, -24);
cq.select(orderRoot).where(
cb.greaterThan(orderRoot.get(Order_.createdAt) ,c.getTime())
);
TypedQuery<Order> query = em.createQuery(cq);
List<Order> result = query.getResultList();
但是,在这种情况下,我需要使用代表这个最大周期的字段 hoursMax 我应该放入什么????以下 ?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Order> cq = cb.createQuery(Order.class);
Root<Order> orderRoot = cq.from(Order.class);
cq.select(orderRoot).where(
cb.greaterThan(orderRoot.get(Order_.createdAt) , ??? )
);
TypedQuery<Order> query = em.createQuery(cq);
List<Order> result = query.getResultList();
注意:另外,我想避免对 hoursMax 执行额外的预查询。