What collections does jpa return?

旧巷老猫 提交于 2019-12-20 03:56:26

问题


Does JPA ( Eclipselink in this case) always return IndirectList where Entity have a List? Is ok that list or It should be converted to another list( maybe linkedlist)?


回答1:


Analysis

If we look at EclipseLink's IndirectList's API, it says:

To use an IndirectList: declare the appropriate instance variable with type IndirectList (jdk1.1) or Collection/List/Vector (jdk1.2).

TopLink will place an IndirectList in the instance variable when the containing domain object is read from the datatabase. With the first message sent to the IndirectList, the contents are fetched from the database and normal Collection/List/Vector behavior is resumed.

If we view IndirectList sources, we will see, that all the work is delegated to it's original collection, just like API says.

Answers

Does JPA ( Eclipselink in this case) always return IndirectList where Entity have a List?

Yes, it always does return your specified collection wrapped with IndirectList. Since it delegates all its internal work to the wrapped collection, it preserves the way it works.

Is ok that list or It should be converted to another list( maybe linkedlist)?

Yes, it is okay to use IndirectList. You don't convert, you just define any type of collection you want and don't worry about IndirectList, since it is managed transparently.




回答2:


Since List is an interface the JPA provider is free to return any implementation. EclipseLink rerurns an IndirectList where a List is used. This is perfectly fine since the IndirectList is a List.

For the record or for future reference, it is generally best practice to use interfaces with JPA.



来源:https://stackoverflow.com/questions/10972971/what-collections-does-jpa-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!