@OneToMany 列表<>vs 集合不同之处 [英] @OneToMany List<> vs Set<> difference

查看:24
本文介绍了@OneToMany 列表<>vs 集合不同之处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用有什么不同吗

Is there any difference if I use

@OneToMany
public Set<Rating> ratings;

或者如果我使用

@OneToMany
public List<Rating> ratings;

两者都可以正常工作,我知道列表和集合之间的区别,但是我不知道这是否对休眠(或者更确切地说 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.

当您必须在实体上定义 equalshashCode 并且实体中没有不可变的功能键时,通常很难将 Set 与 Hibernate 一起使用.

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 列表&lt;&gt;vs 集合不同之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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