I have figured out that I can use hibernate to get the sum of a number of entities using HQL as follows...
public Long getEnvelopeTotal(AbstractEnvelope envelope) {
String query = "select sum(t.amount) from User_Transaction t";
Long result = (Long) hibernateUtil.getSession().createQuery(query).uniqueResult();
return result;
}
Currently the rest of my application is able to seamlesly navigate the database via the object graph only. The problem with having to use the function above is that I have to do the following psuedo code...
- Get instance of Entity "Envelope"
- Pass Envelope instance i
- Based on the result of getEnvelopeTotal set the Envelope instance property "total" to the result.
What I am wondering if it is possible to use hibernate in such a way that the property "total" is set via custom HQL query instead of being mapped to a simply database column.
ex:
@SomeMagicAnnotation(query="select sum(t.amount) from User_Transaction t")
private Long total;
Any suggestions?