休眠中Set和Collection之间的区别

时间:2022-03-15 03:12:45

What is the difference between using a Set or a Collection for @OneToMany or @ManyToMany properties on my hibernate entity objects?

在我的hibernate实体对象上为@OneToMany或@ManyToMany属性使用Set或Collection有什么区别?

Does Hibernate map things differently depending on which one you choose?

Hibernate会根据您选择的内容以不同的方式映射事物吗?

5 个解决方案

#1


In the context of hibernate, following is the scenario under which you would use Set instead of Collection: -

在hibernate的上下文中,下面是使用Set而不是Collection的场景: -

"From Order as orders left fetch join orders.orderLineItems as orderLineItems ORDER BY orders.id DESC"

“从订单到订单时,将订单加入订单,订单连接订单,订单连接订单,订单连续订单。订单。

It returns duplicates so use the hash set to remove them.

它返回重复项,因此使用哈希集删除它们。

Query query = session.getNamedQuery("OrdersAndLoadLineItems"); Set set = new LinkedHashSet(); set.addAll(query.list());
return set;

查询query = session.getNamedQuery(“OrdersAndLoadLineItems”); Set set = new LinkedHashSet(); set.addAll(query.list());返回集;

This is taken from Hibernate FAQ link is http://www.hibernate.org/117.241.html

这取自Hibernate FAQ链接是http://www.hibernate.org/117.241.html

#2


If you look at the API Set extends Collection. According, to the description the Set does not allow null values.

如果你看一下API Set extends Collection。根据描述,Set不允许空值。

#3


Collection is an Interface, cannot be instantiated. Set is also an Interface.

Collection是一个接口,无法实例化。 Set也是一个接口。

As such, it doesn't matter what you use, as long as the instantiated object that you use is compatible with those.

因此,只要您使用的实例化对象与这些对象兼容,您使用的内容无关紧要。

So normally, you would do something like this:

通常情况下,你会做这样的事情:

private Set parts = new HashSet();

#4


A Collection is something more general than a Set. A Set is a more specific subinterface of a Collection. See here.

集合比集合更通用。 Set是Collection的更具体的子接口。看这里。

#5


I don't know the difference, but if you use Set you can fetch multiple bags in JPA, but if you use List, for instance, you cannot fetch multiple bags in a query.

我不知道区别,但是如果你使用Set你可以在JPA中获取多个包,但是如果你使用List,你就不能在查询中获取多个包。

#1


In the context of hibernate, following is the scenario under which you would use Set instead of Collection: -

在hibernate的上下文中,下面是使用Set而不是Collection的场景: -

"From Order as orders left fetch join orders.orderLineItems as orderLineItems ORDER BY orders.id DESC"

“从订单到订单时,将订单加入订单,订单连接订单,订单连接订单,订单连续订单。订单。

It returns duplicates so use the hash set to remove them.

它返回重复项,因此使用哈希集删除它们。

Query query = session.getNamedQuery("OrdersAndLoadLineItems"); Set set = new LinkedHashSet(); set.addAll(query.list());
return set;

查询query = session.getNamedQuery(“OrdersAndLoadLineItems”); Set set = new LinkedHashSet(); set.addAll(query.list());返回集;

This is taken from Hibernate FAQ link is http://www.hibernate.org/117.241.html

这取自Hibernate FAQ链接是http://www.hibernate.org/117.241.html

#2


If you look at the API Set extends Collection. According, to the description the Set does not allow null values.

如果你看一下API Set extends Collection。根据描述,Set不允许空值。

#3


Collection is an Interface, cannot be instantiated. Set is also an Interface.

Collection是一个接口,无法实例化。 Set也是一个接口。

As such, it doesn't matter what you use, as long as the instantiated object that you use is compatible with those.

因此,只要您使用的实例化对象与这些对象兼容,您使用的内容无关紧要。

So normally, you would do something like this:

通常情况下,你会做这样的事情:

private Set parts = new HashSet();

#4


A Collection is something more general than a Set. A Set is a more specific subinterface of a Collection. See here.

集合比集合更通用。 Set是Collection的更具体的子接口。看这里。

#5


I don't know the difference, but if you use Set you can fetch multiple bags in JPA, but if you use List, for instance, you cannot fetch multiple bags in a query.

我不知道区别,但是如果你使用Set你可以在JPA中获取多个包,但是如果你使用List,你就不能在查询中获取多个包。