java, hibernate: mapping property with a query

blow picture blow · Feb 6, 2011 · Viewed 17.6k times · Source

I have an entity with a property "quantity", this value is not a table field but is dynamically calculated with an hql query.

So, is possibile to add this value in my entities and make hibernate calculate it when I load my entities?

Answer

danny.lesnik picture danny.lesnik · Feb 6, 2011

You an use formula annotation. for example:

@Formula("(select min(o.creation_date) from Orders o where o.customer_id = id)")
private Date firstOrderDate;

See this post: Calculated property with JPA / Hibernate

Se also Hibernate tutorial: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-column