Which Java Type do you use for JPA collections and why?

后端 未结 5 1184
走了就别回头了
走了就别回头了 2020-12-22 23:20

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

  • java.util.Collection
  • java.util.List
5条回答
  •  渐次进展
    2020-12-23 00:08

    I generally use a List. I find the List API far more useful and compatible with other libraries than Set. List is easier to iterate and generally more efficient for most operations and memory.

    The fact that a relationship cannot have duplicates and is not normally ordered should not require usage of a Set, you can use whatever Collection type is most useful to your application.

    It depends on your model though, if it is something you are going to do a lot of contains checks on, then a Set would be more efficient.

    You can order a relationship in JPA, either using an @OrderBy or an @OrderColumn.

    See, http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Ordering

    Duplicates are not generally supported in JPA, but some mappings such as ElementCollections may support duplicates.

提交回复
热议问题