I have a entity with a entity called Deadline with a @OneToMany relationship to a entity called DeadlineDate.
Deadline entity:
@Entity
@Table(name="deadline")
@Cache(alwaysRefresh=true)
public class Deadline implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
@OneToMany(mappedBy = "deadline", cascade = PERSIST)
@OrderBy("id ASC")
private List<DeadlineDate> datesList = new ArrayList<DeadlineDate>();
//...
}
DeadlineDate entity:
@Entity
@Table(name="deadlinedate")
public class DeadlineDate implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id")
private Long id;
@Basic
@Column(name = "date", nullable = false)
@Temporal(DATE)
private Date date;
@ManyToOne
@JoinColumn(nullable = false, name = "deadline_id")
private Deadline deadline;
//...
}
The getAllDeadlines method:
private List<Deadline> getAllDeadlines(){
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Deadline> query = criteriaBuilder.createQuery(Deadline.class);
Root<Deadline> root = query.from(Deadline.class);
Join<Deadline, DeadlineDate> joinDeadDate = root.join("datesList");
List<Predicate> predicateList = new ArrayList<Predicate>();
Date date = new Date();
//other unrelated predicates
//i'm stuck here - begin
predicateList.add( criteriaBuilder.lessThan(joinDeadDate.<predicateArray>get("date"), date));
//i'm stuck here - end
Predicate[] predicateArray = new Predicate[predicateList.size()];
predicateList.toArray(predicateArray);
query.where(predicateArray);
TypedQuery<Deadline> typedQuery = em.createQuery(query);
return typedQuery.getResultList();
}
I want to make a query of deadlines that have passed the current system date. The deadlines have a history that stores changes made by the user (expiry dates can be modified forward or backward). Current deadline date is considered the largest id on the deadlinedate table. I'm trying to use the Criteria api, but do not know how to make it return only the last value of the datesList to use in the lessThan method of the criteriaBuilder. Using the current code, i get all records of deadline dates of a particular deadline.
Thanks in advance!