-->
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.  [ 3 posts ] 
Author Message
 Post subject: Order by combined columns question
PostPosted: Sun Oct 31, 2010 9:57 pm 
Newbie

Joined: Sun Oct 31, 2010 9:44 pm
Posts: 2
Hi folks,

I can't figure out how to do the following... maybe I'm getting a little tired :-(

I have a table named 'CLIENT'. Among it's columns, there are 'businessName', 'contactLastName' and 'contactFirstName'.
All of them can be null but contactLastName and contactFirstName cannot be null if businessName is null.

I want to order by a combination of those three columns :
In the UI, a sortable list shows a column named 'Name' that contains the businessName OR contactLastName + " " + contactFirstName if businessName is empty.

I want to order by that column... ideas, suggestions?

Thank you in advance!


Top
 Profile  
 
 Post subject: Re: Order by combined columns question
PostPosted: Mon Nov 01, 2010 12:13 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
In the UI, a sortable list shows a column named 'Name' that contains the businessName OR contactLastName + " " + contactFirstName if businessName is empty.


You have a number of options:

1. Add a property to your entity that is based on a Formula. In your case this formula would be (assuming a function NVL/similar exists in the database you use) - NVL(businessName, contactLastName || " " || contactFirstName). And then use this field in your Criteria

Code:
@Formula(value="NVL(businessName, contactLastName || ' ' || contactFirstName)")
String name
...
s.createCriteria(Client.class).addOrder(Property.forName("name").asc()).list()


Note: The formula is evaluated only when the entity is loaded but from what you say this approach should work out ok for you.

2. If you use a projection based result-set then you can have this formula added to the projection list.

Code:
        Projection projection = Projections.sqlProjection("NVL(businessName, contactLastName || " " || contactFirstName) as name", new String[]{"name"}, new Type[]{Hibernate.STRING});
        Projection projectionAlias = Projections.alias(projection, "name");
        Criteria crit = s.createCriteria(Client.class)
                .setProjection(Projections.projectionList()
                .add(projectionAlias));

        crit.addOrder(Order.asc("usr"));


3. Use HQL. Again use the formula in the projection list and order by that.

Code:
s.createQuery("select NVL(businessName, contactLastName || " " || contactFirstName) as name, …
from Client order by 1").list();


HTH.


Top
 Profile  
 
 Post subject: Re: Order by combined columns question
PostPosted: Mon Nov 01, 2010 7:52 pm 
Newbie

Joined: Sun Oct 31, 2010 9:44 pm
Posts: 2
Thanks, it did help!

I'm using a MySQL database... if it can someday be helpful here's the formula I used

@Formula(value="IFNULL(businessName, CONCAT(contactLastName, ' ', contactFirstName))")


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.