-->
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.  [ 13 posts ] 
Author Message
 Post subject: Unique/Distinct resultset and Criteria
PostPosted: Mon Jun 14, 2004 12:49 am 
Newbie

Joined: Mon Jun 14, 2004 12:01 am
Posts: 4
Hi All

I've been mucking with the Criteria API and I have a question. Basically, I've got a list of Users (Person class) that each have a Set of Traits. I have a query screen that allows me to checkbox different Traits to search by.

Code:
Criteria personCrit = session.createCriteria(Person.class);
Criteria traitCrit = personCrit.createCriteria("profileTraits");

personCrit.setMaxResults(pageSize);
personCrit.setFirstResult((pageNum-1)*pageSize);


It's going to be a pageable resultset, so I set first and max results.
Now I get my ids' from my form and put them into the Trait expression.

Code:

Integer[] choices = form.getIntegerArray(key);

// loop through and
for (int i = 0; i < choices.length; i++)
{
   traitCrit.add(Expression.eq("traitID", choices));
}



The big problem here is that the query is returning one of the main objects (Person) for each trait that the person matches! This makes sense in SQL:

Code:
select a.* from person a, person_trait b where
a.personid = b.personid and
b.trait_id in (1,2,3)


but this is not really the ideal query. What I'm REALLY after is

Code:
select UNIQUE a.* from person a, person_trait b where
a.personid = b.personid and
b.trait_id in (1,2,3)


Note that I can't do "postprocessing" on the list that I can do since this would screw up the pagesize and pagenum.

What is the solution? I can't find anything in the API that deals with this!

Thanks

- Jason


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 1:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Note that I can't do "postprocessing" on the list that I can do since this would screw up the pagesize and pagenum.


Well, thats the problem really, pagination is applied at the database level. There is no way to achieve distinctness, and still get the pagesizes correct.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 1:36 am 
Newbie

Joined: Mon Jun 14, 2004 12:01 am
Posts: 4
gavin wrote:
Quote:
Note that I can't do "postprocessing" on the list that I can do since this would screw up the pagesize and pagenum.


Well, thats the problem really, pagination is applied at the database level. There is no way to achieve distinctness, and still get the pagesizes correct.


But, regardless of the pagesize, is there a way to SELECT DISTINCT using the Criteria API?

Thanks!

- Jason
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 2:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, not yet, but there should be and its trivial, we should implement this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 2:32 am 
Newbie

Joined: Mon Jun 14, 2004 12:01 am
Posts: 4
gavin wrote:
No, not yet, but there should be and its trivial, we should implement this.


Thanks Gavin - I'll look into doing something with HSQL instead.

BTW, you guys have done great work with Hibernate!

- Jason


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 8:00 pm 
Newbie

Joined: Fri Jun 25, 2004 4:25 pm
Posts: 4
I was wondering, has the distinct functionality been implemented into the criteria queries? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 1:52 pm 
Beginner
Beginner

Joined: Fri Mar 19, 2004 7:21 am
Posts: 20
Any progress on this yet? I have run into the same problem with repeated results in a paginated resultset. As such I cant use DISTINCT_ROOT_ENTITY because my count and pagesizes go out the window.

I'm using 3.0.5..

Regards,
Damon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 3:15 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
I too would like to use "distinct" in my criteria queries. Any update on the status of this? Is my only other option to use HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 3:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Projections.distinct(myProjection)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 4:10 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
Thank you. Would you humor me and show me a brief example how I can create a projection that will apply "distinct" to my result set? I can't seem to get it from the apis.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 5:57 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
I figured it out ;) Thanks again.


Top
 Profile  
 
 Post subject: Unique resultset/criteria: Projections.distinct
PostPosted: Thu Jul 07, 2005 12:32 pm 
Newbie

Joined: Thu Jun 09, 2005 12:47 pm
Posts: 5
So, how did you end up using Projections.distinct ? I'm having the same problem you had.

Thanks in Advance,
Jacob


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 1:34 pm 
Newbie

Joined: Thu Apr 15, 2004 4:31 pm
Posts: 7
We are also at a similar dead end. If you have to use a projection in the distinct projection, how do you make it return just the entities you are trying to select in the criteria?


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