Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 204
Ashish Pancholi Avatar asked Sep 21 '17 09:09

Ashish Pancholi


1 Answers

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
like image 187
Anatoly Shamov Avatar answered Oct 16 '22 09:10

Anatoly Shamov