开发者

Hibernate/HQL - how to get all duplicates returned from database?

开发者 https://www.devze.com 2022-12-20 03:32 出处:网络
I\'ve got a table containing some duplicates (defined as some specific columns contain the same values). What\'s the best way to get all those rose back? I need all the duplictes, so group by in开发者

I've got a table containing some duplicates (defined as some specific columns contain the same values). What's the best way to get all those rose back? I need all the duplictes, so group by in开发者_Python百科 combination with having count() > 1* is not the way I'd like to go.

So if my table contains the following data

1 - foo - bar - something
2 - foo - bar - another thing
3 - foo - bar - something
4 - foo - bar - something else

I'd like to get returned:

1 - foo - bar - something
3 - foo - bar - something

Thanks a lot for helping!

Stefan


  1. Fetch all items (SELECT * FROM ..) in a List
  2. Create a new HashBag, passing the list in constructor
  3. get only the items where getCount() is more than 1.

This will work if you have mapped an object to the table, whose equals() method returns true if all the properties are the same.


Another way is to use a subselect. The subselect being your GROUP BY + COUNT() query, and the outer query comparing with the results of the subquery.

0

精彩评论

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