列表与设置JPA 2 - 优点/缺点/便利性 [英] List vs Set on JPA 2 - Pros / Cons / Convenience

查看:89
本文介绍了列表与设置JPA 2 - 优点/缺点/便利性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在Stack Overflow和其他网站上搜索有关使用列表的优点,缺点和便利性,但实际上找不到DEFINITE的答案何时使用这个或那个。



从Hibernate的文档中,他们声明非重复记录应该进入集合,并从那里开始应该为每个可以包装到 Set 中的实体实现hashCode()和equals()。但是,它涉及到便利和易用性的代价,因为有一些文章推荐使用业务密钥作为每个实体的id,并且从那里开始,hashCode()和equals()可以在每种情况下得到完美实现无论对象的状态如何(托管,分离等)。



没关系,一切正常......直到我遇到很多情况时,使用< (尽管Hibernate提供了 SortedSet 的概念),但是setObj.get(index),collectionObj.remove(int location ||)的方便性是不可行的。 Object obj),Android的ListView / ExpandableListView(GroupIds,ChildIds)体系结构和...我的观点是:集合对于操作并使其工作100%非常糟糕(imho) p>

我很想将我的项目的每个集合都更改为 List ,因为它们工作得很好。我的所有实体的ID都是通过MYSQL的自动生成序列( @GeneratedValue(strategy = GenerationType.IDENTITY))生成的。



是否有人能够以一种明确的方式在上述所有这些小细节中清楚我的想法?

另外,是否可以使用Eclipse的自动生成的hashCode()和equals()为每个实体的ID字段?它会在任何情况下都有效吗?



非常感谢,



Renato

解决方案

一个列表,如果没有指定索引列,只会被Hibernate处理为一个包(没有特定的顺序)。 b
$ b

  @OneToMany 
@OrderBy(lastname ASC)
public List< Rating>评级;

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



当你必须在实体上定义equals和hashCode,并且在实体中没有不可变的功能键时,通常很难使用Hibernate的集合。



此外,我建议你这链接


I have tried searching on Stack Overflow and at other websites the pros, cons and conveniences about using Sets vs Lists but I really couldn't find a DEFINITE answer for when to use this or that.

From Hibernate's documentation, they state that non-duplicate records should go into Sets and, from there, you should implement your hashCode() and equals() for every single entity that could be wrapped into a Set. But then it comes to the price of convenience and ease of use as there are some articles that recommend the use of business-keys as every entity's id and, from there, hashCode() and equals() could then be perfectly implemented for every situation regardless of the object's state (managed, detached, etc).

It's all fine, all fine... until I come across on lots of situations where the use of Sets are just not doable, such as Ordering (though Hibernate gives you the idea of SortedSet), convenience of collectionObj.get(index), collectionObj.remove(int location || Object obj), Android's architecture of ListView/ExpandableListView (GroupIds, ChildIds) and on... My point is: Sets are just really bad (imho) to manipulate and make it work 100%.

I am tempted to change every single collection of my project to List as they work very well. The IDs for all my entities are generated through MYSQL's auto-generated sequence (@GeneratedValue(strategy = GenerationType.IDENTITY)).

Is there anyone out the who could in a definite way clear up my mind in all these little details mentioned above?

Also, is it doable to use Eclipse's auto-generated hashCode() and equals() for the ID field for every entity? Will it be effective in every situation?

Thank you very much,

Renato

解决方案

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

@OneToMany
@OrderBy("lastname ASC")
public List<Rating> ratings;

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.

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.

furthermore i suggest you this link.

这篇关于列表与设置JPA 2 - 优点/缺点/便利性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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