简体   繁体   English

Hibernate / HQL-如何获取数据库返回的所有重复项?

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

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 combination with having count( ) > 1* is not the way I'd like to go. 我需要所有的duplictes,所以组份按具有计数()> 1 *是不是我想要走的路。

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 取所有( SELECT * FROM .. )在一个List
  2. Create a new HashBag , passing the list in constructor 创建一个新的HashBag ,在构造函数中传递列表
  3. get only the items where getCount() is more than 1. 仅获取getCount()大于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. 如果已将对象映射到表,则该方法将起作用,如果所有属性都相同,则其equals()方法将返回true


Another way is to use a subselect . 另一种方法是使用subselect The subselect being your GROUP BY + COUNT() query, and the outer query comparing with the results of the subquery. 子选择是您的GROUP BY + COUNT()查询,而外部查询则与子查询的结果进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM