如何构建动态查询,添加日期天数并使用条件API将该日期与另一个日期进行比较?
作者:互联网
我有一张A桌和一张B桌. JPA实体,A和B类都有joda.time.datetime持久字段分别表示retentionDate和lastmodifiedDate. A还有一个类型为int的持久字段.现在我想将a.days的数量添加到a.retentionDate中,然后使用JPA criteria API将其与b.lastmodifiedDate进行比较.
实体类
@Entity
@Table(name = "A")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class A extends AbstractBaseEntity {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(unique = true, length = 36)
private String id;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonDeserialize(using = DateTimeDeserializer.class)
@JsonSerialize(using = DateTimeSerializer.class)
private DateTime retentionDate;
private int days;
}
B实体类
@Entity
@Table(name = "B")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class B extends AbstractBaseEntity {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(unique = true, length = 36)
private String id;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonDeserialize(using = DateTimeDeserializer.class)
@JsonSerialize(using = DateTimeSerializer.class)
private DateTime lastmodifiedDate ;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "A_ID")
private A a;
}
JPA标准查询:
CriteriaQuery<B> b = builder.createQuery(B.class);
Root<B> fromB = criteria.from(B.class);
Expression<DateTime> lastmodifiedDate = fromB.get(B_.lastmodifiedDate);
Expression<DateTime> retentionDate = fromB.get(B_.a).get("retentionDate");
Expression<int> days = fromB.get(B_.a).get("days");
Expression<DateTime> newDate = // how to add days in retentionDate here so it became a newDate?
b.select(fromB);
b.where(builder.greaterThanOrEqualTo(newDate , lastmodifiedDate)
);
请帮助我在上面的查询中添加retentionDate中的天数.谢谢.
解决方法:
您可以使用CriteriaBuilder#function()执行数据库功能以将天数添加到retentionDate中.
我用DATEADD()做了一个例子,因为我的测试项目是用H2数据库运行的.
ParameterExpression<String> dayUnit = builder.parameter(String.class, "dayUnit");
Expression<DateTime> newDate = builder.function("DATEADD", DateTime.class, dayUnit, days, retentionDate);
完整的例子:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<B> cq = builder.createQuery(B.class);
Root<B> fromB = cq.from(B.class);
Expression<DateTime> lastmodifiedDate = fromB.get(B_.lastmodifiedDate);
Expression<DateTime> retentionDate = fromB.get(B_.a).get("retentionDate");
Expression<Integer> days = fromB.get(B_.a).get("days");
ParameterExpression<String> dayUnit = builder.parameter(String.class, "dayUnit");
Expression<DateTime> newDate = builder.function("DATEADD", DateTime.class, dayUnit, days, retentionDate);
cq.where(builder.greaterThanOrEqualTo(newDate, lastmodifiedDate));
TypedQuery<B> tq = entityManager.createQuery(cq.select(fromB));
tq.setParameter("dayUnit", "DAY");
List<B> results = tq.getResultList();
此查询转换为SQL为:
select
b0_.id as id1_1_,
b0_.a_id as a_id3_1_,
b0_.lastmodified_date as lastmodi2_1_
from b b0_
cross join a a1_
where b0_.a_id=a1_.id
and DATEADD(?, a1_.days, a1_.retention_date)>=b0_.lastmodified_date
标签:java,jpa,hibernate,hibernate-criteria,criteria-api 来源: https://codeday.me/bug/20190701/1348897.html