What collections does jpa return?

后端 未结 2 1623
清酒与你
清酒与你 2021-01-25 19:02

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)?

相关标签:
2条回答
  • 2021-01-25 19:28

    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.

    0 讨论(0)
  • 2021-01-25 19:29

    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.

    0 讨论(0)
提交回复
热议问题