您为JPA集合使用哪种Java类型,为什么? [英] Which Java Type do you use for JPA collections and why?

查看:75
本文介绍了您为JPA集合使用哪种Java类型,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您在JPA域模型中使用以下哪些收集类型以及原因:

Which of the following collection types do you use in your JPA domain model and why:

  • java.util.Collection
  • java.util.List
  • java.util.Set
  • java.util.Collection
  • java.util.List
  • java.util.Set

我想知道是否有一些基本规则.

I was wondering whether there are some ground rules for this.

更新,我知道SetList之间的区别. List允许重复并且具有顺序,而Set不能包含重复元素并且不定义顺序.我在JPA的背景下问这个问题.如果严格遵循定义,那么您应该始终使用Set类型,因为您的集合存储在关系数据库中,在这里您不能重复,并且您可以自己定义订单,即您的Java List不一定保留在数据库中.

UPDATE I know the difference between a Set and a List. A List allows duplicates and has an order and a Set cannot contain duplicate elements and does not define order. I'm asking this question in the context of JPA. If you strictly follow the definition, then you should always end up using the Set type, since your collection is stored in relational database, where you can't have duplicates and where you have define an order by yourself, i.e. the order in you Java List is not necessarily preserved in the DB.

例如,大多数时候我使用List类型,不是因为它有订单或允许重复(无论如何我都不能拥有),因为我的组件库中的某些组件需要一个列表.

For example, most of the time I'm using the List type, not because it has an order or allows duplicates (which I can't have anyway), because some of the components in my component library require a list.

推荐答案

就像您自己的问题所示,关键是域,而不是JPA . JPA只是一个可以(并且应该)以最适合您的问题的方式使用的框架.由于框架(或其局限性)而选择次优解决方案通常是一个警告.

Like your own question suggests, the key is the domain, not JPA. JPA is just a framework which you can (and should) use in a way which best fits your problem. Choosing a suboptimal solution because of framework (or its limits) is usually a warning bell.

当我需要一套而不关心订单时,我会使用Set.如果出于某种原因,顺序很重要(排序列表,按日期排序等),则List.

When I need a set and never care about order, I use a Set. When for some reason order is important (ordered list, ordering by date, etc.), then a List.

您似乎很清楚CollectionSetList之间的区别.使用一个vs.另一个的唯一原因仅取决于您的需求.您可以使用它们与您的API(或您将来的自己)的用户交流您集合的属性()(可能是微妙的或隐含的).

You seem to be well aware of the difference between Collection, Set, and List. The only reason to use one vs. the other depends only on your needs. You can use them to communicate to users of your API (or your future self) the properties of your collection (which may be subtle or implicit).

这与在整个代码中其他任何地方使用不同集合类型时遵循的规则完全相同.您可以为所有引用使用ObjectCollections,但是在大多数情况下,您将使用更具体的类型.

This is follows the exact same rules as using different collection types anywhere else throughout your code. You could use Object or Collections for all your references, yet in most cases you use more concrete types.

例如,当我看到List时,我知道它是以某种方式排序的,在这种情况下,重复项是可以接受的还是无关紧要的.当我看到Set时,通常希望它没有重复且没有特定的顺序(除非它是SortedSet).当我看到Collection时,除了包含一些实体之外,别无所求.

For example, when I see a List, I know it comes sorted in some way, and that duplicates are either acceptable or irrelevant for this case. When I see a Set, I usually expect it to have no duplicates and no specific order (unless it's a SortedSet). When I see a Collection, I don't expect anything more from it than to contain some entities.

关于列表排序...是的,可以保留.即使不是,而您只使用@OrderBy,它仍然会很有用.考虑一下默认情况下按时间戳排序的事件日志示例.人为地对列表进行重新排序没有什么意义,但是默认情况下对列表进行排序仍然很有用.

Regarding list ordering... Yes, it can be preserved. And even if it's not and you just use @OrderBy, it still can be useful. Think about the example of event log sorted by timestamp by default. Artificially reordering the list makes little sense, but still it can be useful that it comes sorted by default.

这篇关于您为JPA集合使用哪种Java类型,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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