Java Hibernate:最好使用的集合类型 - bag、idbag、set、list、map
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1505874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: best collection type to use - bag, idbag, set, list, map
提问by Nemi
I am looking for what most people use as their collection type when making one-to-many associations in Hibernate. The legacy application I am maintaining uses bags exclusively, but keeps them as lists in code. The tables associated have an id field, so an idbag seems more appropriate, but documentation recommends a Set.
我正在寻找大多数人在 Hibernate 中进行一对多关联时使用的集合类型。我正在维护的遗留应用程序专门使用包,但将它们作为列表保存在代码中。关联的表有一个 id 字段,因此 idbag 似乎更合适,但文档建议使用 Set。
EDIT: I mistakenly referenced that the documentation recommends a set. In reality, the official documentation is equally vague on all collection types. What I find is that somewebsitesseem to infer that Set is the most common, and the Hibernate book I am reading explicitly says this about sets:
编辑:我错误地引用了文档推荐了一组。实际上,官方文档对所有集合类型都同样含糊不清。我发现有些网站似乎推断 Set 是最常见的,而我正在阅读的 Hibernate 书明确说明了关于集合的内容:
This is the most common persistent collection in a typical Hibernate application. (see: page 242 of 'Java Persistence with Hibernate' by Christian Bauer and Gavin King)
这是典型 Hibernate 应用程序中最常见的持久化集合。(参见:Christian Bauer 和 Gavin King 撰写的“Java Persistence with Hibernate”的第 242 页)
I guess that is what threw me and made me seek out what others are using.
我想这就是让我失望并让我寻找其他人正在使用的东西的原因。
EDIT2: note that Gavin King is the creator of Hibernate
EDIT2:注意 Gavin King 是 Hibernate 的创造者
采纳答案by Nemi
Ok, after quite some time I have found a reason NOT to use a Set as a collection type. Due to problems with the hashcode/equals overrides and the way hibernate persists, using any java API functionality that calls hashcode/equals is a bad idea. There is no good way to consistently compare objects pre- and post-persistence. Stick with collections that do not rely on equals/hashcode like bag
.
好的,经过一段时间后,我找到了不使用 Set 作为集合类型的原因。由于 hashcode/equals 覆盖的问题和休眠持续的方式,使用任何调用 hashcode/equals 的 java API 功能都是一个坏主意。没有什么好的方法可以一致地比较持久化前后的对象。坚持使用不依赖于 equals/hashcode 之类的集合bag
。
More info here:
更多信息在这里:
http://community.jboss.org/wiki/EqualsandHashCode(this link makes it sound like a business key is the way to go, but read the next link fully to see why that is not always a good idea)
http://community.jboss.org/wiki/EqualsandHashCode(此链接听起来像是业务关键是要走的路,但请完整阅读下一个链接以了解为什么这并不总是一个好主意)
https://forum.hibernate.org/viewtopic.php?f=1&t=928172(read the whole discussion to make your head spin)
https://forum.hibernate.org/viewtopic.php?f=1&t=928172(阅读整个讨论让你头晕目眩)
回答by ChssPly76
I'm guessing people use all kinds of things :-) - different collection types serve different purposes so the "best" one depends on what you need it for.
我猜人们使用各种各样的东西:-) - 不同的集合类型有不同的用途,所以“最好的”一个取决于你需要它做什么。
That said, using List
in code is usually more convenient than using Set
even though said List
is unordered. If nothing else, '.get(0)' is easier on the eyes than .iterator().next()
:-) Hibernate bag support is definitely adequate for this purpose plus you can even add an order-by
declaration (if applicable) and have your list sorted.
也就是说,即使 said是无序的,List
在代码中使用通常也比使用更方便。如果不出意外,'.get(0)' 比:-) 对 Hibernate 包的支持绝对足够用于此目的,而且您甚至可以添加声明(如果适用)并对您的列表进行排序。Set
List
.iterator().next()
order-by
idbagis a whole different animal used for many-to-many associations; you can't really compare it to regular Set or List.
idbag是一种完全不同的动物,用于多对多关联;您无法真正将其与常规 Set 或 List 进行比较。
回答by MrWhite
I would recommend using a set because a set is defined as a collection of unique items and thats normally what you deal with.
我建议使用一个集合,因为一个集合被定义为一个独特项目的集合,这通常是你要处理的。
And .iterator().next()
is save when there is no element in your collection.
并且.iterator().next()
在您的集合中没有元素时保存。
.get(0)
might throw an IndexOutOfBoundsException
if you access an empty list.
.get(0)
IndexOutOfBoundsException
如果您访问一个空列表,可能会抛出一个。
回答by Cookalino
Based on the experience of using both I would recommend using a List. If you are getting data out of the database and displaying / manipulating it then it nearly always needs to be kept in a consistent order. You can use SortedSet but that can add a whole world of pain (overriding equals, hashcode etc. and sorting in different ways) compared to just adding an order by and storing it in a List. Lists are easier to manipulate - if a user deletes line 3 on the page, then just remove item 3 in the List. Working with a Set seems to involve lots of unnecessary code and messing about with iterators.
根据使用两者的经验,我建议使用 List。如果您从数据库中获取数据并显示/操作它,那么它几乎总是需要保持一致的顺序。您可以使用 SortedSet ,但与仅添加一个 order by 并将其存储在 List 中相比,这可能会增加整个世界的痛苦(覆盖等号、哈希码等并以不同的方式排序)。列表更容易操作 - 如果用户删除页面上的第 3 行,则只需删除列表中的第 3 行。使用 Set 似乎涉及大量不必要的代码,并与迭代器混为一谈。
When I have used Sets with Hibernate I have frequently found myself ripping all the Sets out after a few weeks and replacing with Lists because Sets are giving me too many limitations.
当我在 Hibernate 中使用 Sets 时,我经常发现自己在几周后撕掉了所有 Sets 并用 Lists 替换,因为 Sets 给了我太多的限制。
The Hibernate documentation and third party tools seem to use Sets by default but from hard experience I have found it much more productive to use Lists.
Hibernate 文档和第三方工具似乎默认使用 Sets,但从艰苦的经验中我发现使用 Lists 效率更高。