-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Is it possible to order by computed columns without formula?
PostPosted: Mon Aug 15, 2005 6:57 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
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


Top
 Profile  
 
 Post subject: Why map?
PostPosted: Mon Aug 15, 2005 11:19 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suggest removing the 'totalBruto' mapping from H mapping and have formula in one place: gettter method of the class

public double getTotalBruto() {
return quantity * unitPrice;
}

And for sorting, simply do it in the code: just implement interface Comparable on the class or provide custom Comparator to the Collections.sort method.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: Why map?
PostPosted: Mon Aug 15, 2005 11:43 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
kgignatyev wrote:
And for sorting, simply do it in the code: just implement interface Comparable on the class or provide custom Comparator to the Collections.sort method.


That's not going to work if you need to fetch paged results with .setFirstResult() / .setMaxResults().

My first thoughts were to try a custom UserType or by treating a TotalBruto class as a mapped component. I haven't explored this any deeper though to see if this is even possible. Certainly the mapping part isn't hard, but getting the order correct seems non-trivial.

My other thought is that for this specific case it probably doesn't matter. What are the chances that the definition of a totalBruto is ever going to change? From the description given it seems highly unlikely and not really a maintenance crisis if it does.


Top
 Profile  
 
 Post subject: Is it possible to order by computed columns without formula?
PostPosted: Mon Aug 15, 2005 10:54 pm 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
jdl wrote:
kgignatyev wrote:
And for sorting, simply do it in the code: just implement interface Comparable on the class or provide custom Comparator to the Collections.sort method.


That's not going to work if you need to fetch paged results with .setFirstResult() / .setMaxResults().

My first thoughts were to try a custom UserType or by treating a TotalBruto class as a mapped component. I haven't explored this any deeper though to see if this is even possible. Certainly the mapping part isn't hard, but getting the order correct seems non-trivial.

My other thought is that for this specific case it probably doesn't matter. What are the chances that the definition of a totalBruto is ever going to change? From the description given it seems highly unlikely and not really a maintenance crisis if it does.


Thanks for your responses,

The formula I presented above is just a simple formula to describe my point. Actually in the real world I have a lot more complex formulas which are computed from a lot of fields in the class. My temporary solution now is to use the later approach and not give the user an option to order by the total bruto, but I don't like this solution since it will reduce the user-friendliness of my applications.

Any solutions are welcome.

Regards,

Setya


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.