-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to Select Distinct Values
PostPosted: Thu Oct 18, 2007 9:54 am 
Beginner
Beginner

Joined: Mon Oct 01, 2007 8:21 am
Posts: 40
Hi,

How to select the distinct rows with the particular field like there is a particular field say "type" with that i have "TT" field value repeated twice where it should get only the distinct value with the row instead of fetching all the rows.

How can this be acheived using the criteria queries or HQL.

Pls Anyone help me in this.

Thanks,
Ambika.


Top
 Profile  
 
 Post subject: Use projections.
PostPosted: Sat Oct 20, 2007 3:48 am 
Newbie

Joined: Mon Feb 20, 2006 1:40 am
Posts: 7
Location: Hyderabad
Normally, hibernate fetches information from database distinctly only on the mapped <id> column. If you wish to get distinct based on another column, use projections.

Project all columns you want to build your data class and add a distinct on your column of interest

e.g.

Projection projection = Projections.projectionList();
pojection.add(Projections.property("P1");
pojection.add(Projections.property("P2");
pojection.add(Projections.property("P3");
pojection.add(Projections.property("P4");
.
.
.
pojection.add(Projections.distinct(Projections.property("TT"));

Then set this projection to the criteria.

This would return a List<Object[]>. Iterate this list and build your class. This is however a costly operation and must be avoided.

Alternativley, you may map another class with <id> mapped to the column you want. Make sure you map that with mutable="false" lest you mess around with your data.

Do not forget to post a reply if this answers your question.

==============================

Shreenath Sreenivas
Senior Software Engineer (TL)
Ocimum Biosolutions


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