[hibernate-issues] [Hibernate-JIRA] Commented: (HB-520) Add Distinct to Criteria API

Paul Malolepsy (JIRA) noreply at atlassian.com
Wed Sep 6 19:11:28 EDT 2006


    [ http://opensource.atlassian.com/projects/hibernate/browse/HB-520?page=comments#action_24347 ] 

Paul Malolepsy commented on HB-520:
-----------------------------------

Has there been any more progress on this one?

The only "workaround" for this that I've been able to come up with is to issue two queries from the criteria same criteria object.  The first one gets the id's the second one is contrained to the ids.  

//set up crtieria as you wish, including pagination
myCriteria = doStuffToSetupCriteria();
myCriteria.setFirstResult((page-1)*itemsPerPage);
myCriteria.setMaxResults(itemsPerPage);

//get the list if primary keys
myCriteria.setProjection(Projections.distinct(Projections.property("myAllias.id"));
List ids = gacc.list();

//now add the id's into the restriction            
myCriteria.add(Restrictions.in("myAlias.id, ids));

//clean up from the last critiera run
gacc.setProjection(null);
gacc.setFirstResult(0);
gacc.setMaxResults(Integer.MAX_VALUE);

//your results
List objects = gacc.list()


A little hacky I agree, but the only acceptable soltion I could find given this limitiation.



> Add Distinct to Criteria API
> ----------------------------
>
>          Key: HB-520
>          URL: http://opensource.atlassian.com/projects/hibernate/browse/HB-520
>      Project: Hibernate2
>         Type: New Feature

>   Components: core
>     Versions: 2.1 rc1
>  Environment: Hibernate 2.1+, (Any DB)
>     Reporter: Stephen Owens
>     Priority: Minor

>
>
> Need the ability to specify that results of a Criteria query should be 'DISTINCT', especially when joining to a collection. So the Criteria equivalent of the HQL statement 
> "select distinct parent from Parent as parent join Parent.children as child where child.age>5"

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira




More information about the hibernate-issues mailing list