This is my original HQL query which works just fine.
Code:
protected static final String HQL_FIND_LATEST_DOCS = "from " + DOC_REFERENCE_TABLE_NAME
+ " as docRef1 where docRef1.recipientAccount in (:accounts) and docRef1.ticketNumber = ("
+ " select max(docRef2.ticketNumber) from " + DOC_REFERENCE_TABLE_NAME
+ " as docRef2 where docRef1.recipientAccount = docRef2.recipientAccount and docRef2.transactionDate = ("
+ " select max(docRef3.transactionDate) from " + DOC_REFERENCE_TABLE_NAME
+ " as docRef3 where docRef3.recipientAccount = docRef2.recipientAccount))";
Now I am rewriting all HQL to criteria.
This is what I made of it.
Code:
public List getLatestDocumentReferences(final IBANAccountNumber[] accountNumbers) {
DetachedCriteria maxTrans = DetachedCriteria.forClass(DocumentReference.class, "doc3")
.setProjection(Projections.projectionList().add(Projections.max("doc3.transactionDate")))
.add(Restrictions.eqProperty("doc3.recipientAccount", "doc2.recipientAccount"));
final DetachedCriteria maxTick = DetachedCriteria.forClass(DocumentReference.class, "doc2")
.setProjection(Projections.projectionList().add(Projections.max("doc2.recipientAccount")))
.add(Restrictions.eqProperty("doc1.recipientAccount", "doc2.recipientAccount"))
.add(Property.forName("doc2.transactionDate").eq(maxTrans));
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
return session.createCriteria(DocumentReference.class.getName(), "doc1")
.add(Restrictions.in("doc1.recipientAccount", accountNumbers ))
//.add(Subqueries.in("doc1.ticketNumber", maxTick))
.add(Property.forName("doc1.ticketNumber").eq(maxTick))
.list();
}
});
}
The problem is I keep getting nullpointer-exceptions on my return.
Is there something I am missing (btw: i am fairly new to writing criteria)