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.