views:

39

answers:

1

Hi,

I have a Hibernate class which has a reasonably complicated implementation of equals(). Is it possible to write a query that returns all instances of this class that have at least one other equal object, where equality is defined by the implementation of equals()?

Update Evidently this is not possible. As an alternative, is it possible to retrieve all instances of a particular class that have equal property values. For example:

Get all instances of User that have the same name

Thanks, Don

+3  A: 

Via query? No.

Hibernate will not use your equals() for loading at all, actually, unless your entity is part of collection.

Update (based on question edit):

Yes, that is possible for mapped properties. You'll need to use a group by / having and a subquery. Something like:

from Person p
 where name in (
  select p2.name from Person p2
   group by p2.name
   having count(p2.id) > 1
)
ChssPly76
+1 Hibernate is an ORM, it maps data to your objects, not the other way around.
jcm