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 this seemingly simple query possible?
PostPosted: Tue Mar 31, 2009 5:37 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
Using the latest version of hibernate and mysql is the following query possible?

Table:
Name
------
Id integer auto_increment
First varchar(100)
Middle varchar(100)
Last varchar(100)

Query:
I need to return a list of all distinct names sorted by last name and the first + middle names. Sounds easy, something like this:
SELECT DISTINCT (name.Last, name.First||' '||name.Middle) FROM Name name ORDER BY name.Last, name.First, name.Middle

Actually I don't know if DISTINCT would work that way, but the problem anyway above is that a first name of "Scott" and a middle name of "Mueller" would come before a first name of "Scott Mueller" and a blank or null (both are allowed, blank means it's known to be empty and null means it's unknown) middle name. Those 2 names (given a common last name) should be equal and only returned once. Not only that, but if the second case was "Scott Mueller2" instead, then it should come IMMEDIATELY after the first case. "Scott Allen" should not come in between in the result.

Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 1:52 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Why don't you just give it a try. I believe the order by clause will translate to the same order the database puts it in.

From the hql documentation
Quote:
The list returned by a query may be ordered by any property of a returned class or components:

http://www.hibernate.org/hib_docs/refer ... ering.html

Just try it that would be the fastest way to find out!

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 5:54 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
ethnarch wrote:
Why don't you just give it a try. I believe the order by clause will translate to the same order the database puts it in.

From the hql documentation
Quote:
The list returned by a query may be ordered by any property of a returned class or components:

http://www.hibernate.org/hib_docs/refer ... ering.html

Just try it that would be the fastest way to find out!

Well it logically doesn't make sense. Yes I can order by last name, then first and then middle. But that will place first name "Scott Allen" AFTER first name "Scott" and middle name "Zebra" (given a common last name). It should be the other way around. Even worse, the first case above will not match "Scott" and "Allen", they'll be considered different names.

I'm able to work around this using HQL case statements, but would love a more elegant solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 6:06 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Did you look into comparator's

Using a comparator
http://parasjain01.wordpress.com/2008/1 ... omparator/

or comparator with annotations
http://www.hibernate.org/hib_docs/annot ... /Sort.html

through mappings
http://www.hibernate.org/hib_docs/refer ... ons-sorted

_________________
Please rate my replies as I need points for all of my questions also.


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.