This content has been marked as final.
Show 2 replies
-
1. Re: Computed Columns in Entities
jlessner Mar 1, 2007 6:11 AM (in response to eiben)The only portable way I know is by queries which return both, the actual entity and the computed value as two different result elements, e.g.
EntityManager em;
Query q = em.createQuery("SELECT o, COUNT(o.positions) FROM OrderEntity o ...");
List<Object[]> results = q.getResultList();
for (Object[] result: results) {
OrderEntity order = (OrderEntity)result[0];
order.setPositionCount((Integer)result[1]);
//... do something with your order
}
This is of course somehow selfmade but maybe it helps :-)
Regards,
Jan -
2. Re: Computed Columns in Entities
eiben Mar 2, 2007 2:32 AM (in response to eiben)Hmm, I was afraid someone would suggest something like this.
Well I could also do something likepublic int getItemsCount() { this.getOrderitems().size(); } public float getItemTotal() { float retVal = 0; for (Orderitem currentOrder : this.getOrderitems()) { retVal += currentOrder.getQuantity() . currentOrder.getItem().getPrice(); } return retVal; }
but I would consider this as sub-optimal, because doing this kind of computation could also be done using the database, and I would say that it would be a little bit faster ...