@OneToMany List<> vs Set<>区别 [英] @OneToMany List<> vs Set<> difference

查看:125
本文介绍了@OneToMany List<> vs Set<>区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用

@OneToMany
public Set<Rating> ratings;

或者如果我使用

@OneToMany
public List<Rating> ratings;

两者都运行正常,我知道列表与集合之间的差异,但我不知道这是否hibernate(或者更确切地说是JPA 2.0)如何处理它会有所不同。

both work OK, i know difference between list and a set, however I don't know if this makes any difference how hibernate (or rather JPA 2.0) handles it.

推荐答案

如果没有指定索引列,则列表只会被Hibernate处理为一个包(没有具体的订购)。

A list, if there is no index column specified, will just be handled as a bag by Hibernate (no specific ordering).

Hibernate处理的一个显着区别是你无法在一个查询中获取两个不同的列表。例如,如果您有一个 Person 实体,其中包含联系人列表和地址列表,您将无法使用单个查询来加载所有人联系人及其所有地址。在这种情况下的解决方案是进行两个查询(避免笛卡尔积),或使用 Set 而不是 List 至少有一个集合。

One notable difference in the handling of Hibernate is that you can't fetch two different lists in a single query. For example, if you have a Person entity having a list of contacts and a list of addresses, you won't be able to use a single query to load persons with all their contacts and all their addresses. The solution in this case is to make two queries (which avoids the cartesian product), or to use a Set instead of a List for at least one of the collections.

当你必须定义等于 hashCode ,并且实体中没有不可变的功能键。

It's often hard to use Sets with Hibernate when you have to define equals and hashCode on the entities and don't have an immutable functional key in the entity.

这篇关于@OneToMany List&lt;&gt; vs Set&lt;&gt;区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆