关于堆栈溢出有许多问题,涉及到在Hibernate实体中使用List和Set。我的问题略有不同(也可能是一样的,但我还不能理解)。
如果我读了Hibernate文档,他们建议在这里使用Set进行许多值关联( single/#persistent-classes-equalshashcode )。
Quoting from above documentation link:
You have to override the equals() and hashCode() methods if you:
- intend to put instances of persistent classes in a Set (the recommended way to represent many-valued associations);
- and intend to use reattachment of detached instances那是直到冬眠4.3。但是在以后的版本中,我不再看到这句话了(Guide.html#mapping-model-pojo-equalshashcode)
所以我的问题是
Set而不是List?谢谢!!
发布于 2017-05-02 10:26:30
好吧,回答你的第一个问题:
为什么Hibernate建议在Hibernate 4之前使用Set而不是List?
首先,我将指出List和Set的主要区别
List是一个有序集合,而Set不是,但是请注意,虽然List是一个有序集合,但是如果对象中没有指定索引列,则不会对其排序。Set不允许重复,而List则不允许重复。第二点解释了在List上使用List的建议,即使lists and bags are more efficient than sets也是如此。
实际上,在双向关系中使用List的问题是,当您在父关联上调用合并操作时,它插入重复的子级(这似乎是 in older Hibernate versions ),您可以从以下网站了解更多有关此问题的信息:
Hibernate Facts: Favoring bidirectional Set(s) vs List(s)
回答你的第二个问题:
因为我在Hibernate 5中没有看到这个建议,这是否意味着它不再有效?
我们不能在这个事实中做一个推论,但我认为这个bug是固定在Hibernate 5上的,这就是为什么我们不再看到这个建议的原因,但这只是一个假设。
https://stackoverflow.com/questions/43734021
复制相似问题