Dónal
Dónal

Reputation: 187539

Hibernate query: find equal objects

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

Upvotes: 0

Views: 910

Answers (1)

ChssPly76
ChssPly76

Reputation: 100726

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
)

Upvotes: 3

Related Questions