Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.0.5
Mapping documents:
<hibernate-mapping>
<class name="com.farbeyond.ejb.research.model.OrderDetail"
table="OrderDetails"
...
<property name="totalBruto"
type="double"
formula="Quantity * UnitPrice"
update="false"
insert="false"/>
...
</class>
</hibernate-mapping>
Name and version of the database you are using:
SQL Server 7.0
Hi all,
I need to order records by computed column, in this case the column is 'totalBruto' which is computed using Quantity * UnitPrice formula. To order records I use the following criteria :
...
criteria.addOrder(Order.asc("totalBruto"));
...
So far so good.
The problem is when user changes the field Quantity or UnitPrice I also want to recompute the totalBruto field right away and present it to user instantly. So I have to code the formula somewhere else in the OrderDetails class. I think from maintenance point of view this is cumbersome & error-prone approach because I have to maintain the formula in hibernate mapping & OrderDetails class.
I try to remove the totalBruto setter method and instead of writing the following totalBruto getter :
public double getTotalBruto()
{
return totalBruto;
}
I write the following totalBruto getter which directly returns the formula :
public double getTotalBruto()
{
return quantity * unitPrice;
}
I also remove the property elements of totalBruto field in the hibernate mapping.
The latter approach seems much easier to maintain since I only have to maintain the formula in the getTotalBruto method. But then I won't be able to order records by totalBruto field.
Is there any solutions for this or am I missing something ?
Any help would be greatly appreciated.
Regards,
Setya