How to build a dynamic query which add number of days to date and compare that date with another date using criteria API?

瘦欲@ 提交于 2020-01-02 06:25:14

问题


I've got a A table, and a B table. Both JPA entity, A and B classes has joda.time.datetime Persistent Field say retentionDate and lastmodifiedDate respectively. A has one more Persistent Field of type int says days. Now I would like to add number of a.days into a.retentionDate and then compare it with b.lastmodifiedDate using JPA criteria API.

A Entity class

@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 class

@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 criteria query:

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

Please help me in above query to add days in retentionDate. Thank you.


回答1:


You can use CriteriaBuilder#function() to execute a database function to add days into retentionDate. I've made an example with DATEADD(), as my test project runs with H2 database.

    ParameterExpression<String> dayUnit = builder.parameter(String.class, "dayUnit");
    Expression<DateTime> newDate = builder.function("DATEADD", DateTime.class, dayUnit, days, retentionDate);

And the full example:

    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();

This query transforms into SQL as:

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


来源:https://stackoverflow.com/questions/46340025/how-to-build-a-dynamic-query-which-add-number-of-days-to-date-and-compare-that-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!