开发者

Hibernate query: find equal objects

开发者 https://www.devze.com 2022-12-13 09:14 出处:网络
I have a Hibernate class which has a reasonably complicated implementation of equals(). Is it possibl开发者_如何学编程e to write a query that returns all instances of this class that have at least one

I have a Hibernate class which has a reasonably complicated implementation of equals(). Is it possibl开发者_如何学编程e 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


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
)
0

精彩评论

暂无评论...
验证码 换一张
取 消