-->
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: How to use the Distinct interface with Criteria queries
PostPosted: Mon Feb 11, 2008 9:41 am 
Newbie

Joined: Mon Feb 11, 2008 9:23 am
Posts: 2
Hi,

I have an Email object, which has a list of Recipient objects and I would like to know how to use criteria queries to match the e-mail address of one of the recipients without getting back duplicate objects.

I have searched around a lot and found the Distinct interface (org.hibernate.criterion.Distinct) which I believe was added in Hibernate 3.0.2 to remedy situations like this, but I cannot figure out how to use it. I can't find any examples of use of this class online.

Please could somebody let me know how to use this Distinct interface for use with the following simple code:

Code:
Criteria crit = this.getSession().createCriteria(Email.class);
Criteria recipCrit = crit.createCriteria("recipients");
recipCrit.add(Restrictions.eq("emailAddress", "email@address.com"));


I cannot use the following line because I plan to page the results:

Code:
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 4:14 am 
Beginner
Beginner

Joined: Thu May 17, 2007 9:56 am
Posts: 21
Location: India
If you want to fetch the distinct value of only one field, then you can go for this

recipCrit.setProjection(Projections.distinct("propertyName"));

_________________
Kuzhali


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 5:31 am 
Newbie

Joined: Mon Feb 11, 2008 9:23 am
Posts: 2
Thanks for the advice, kuzhali. I did fiddle around with something similar to what you suggested at the time, but in the end I settled for changing it to an HQL query. This actually turned out better because I could use the distinct keyword (similar to SQL)...

Code:
String hql = "select distinct..."


and I could also just select the fields I needed from the query instead of the whole object which made everything slightly quicker.


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.