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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With