Hibernate version:3.1beta1
Name and version of the database you are using: HSQL-1.8.0.1
Hi there,
I have researching for a while now a way to get distinct entities using the Criteria API.
The only way I have found, so far, is more or less like this:
Code:
Criteria crit = session.createCriteria(Foo.class);
// Adding restrictions to the entities
crit.add(Restrictions.???());
.....
crit.add(Restrictions.???());
// Setting the distinct projection.
crit.setProjection(Projections.distinct( other projection goes in here ));
The problem here is what Projection to use as parameter to Projections.distinct. In this forum I found a post at:
http://forum.hibernate.org/viewtopic.php?t=931731
and the last person to ask is asking exactly what I need.
Quote:
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?
Why is it that Projections.distinct needs to take a Projection for parameter???? or even better; can we have an overloaded version of Projections.distinct that takes no parameter at all?? just like Projections.rowCount()?? or as some one suggested in some of the threads I read before posting; Criteria.listDistinct();
There are many other threads in this forum asking for help regarding this and none of them seem to find a definite solution.
There is one solution that comes to mind that althought it is extremelly inefficient it would work and is to define a ResultTransformer that will take the tuples returned by the Criteria and convert them to the original entity. However, there is functionality in hibernate to do this already... Anyway.. I hope some one can come up with a nice solution.
Cheers 2 you all.
edovale