Persisting LinkedList in Hibernate

后端 未结 2 1331
温柔的废话
温柔的废话 2021-01-07 02:28

I am trying to persist a Class with a LinkedList Attribute but can\'t seem to get it right. Here is my code and my mapping:

 import java.util.LinkedList;
 pu         


        
相关标签:
2条回答
  • 2021-01-07 03:08

    Generally, Hibernate will provide its own implementations for collections so you should prefer interfaces to specific implementations. It's probably attempting to assign a different kind of list to images and failing. You would have to change your field to List<Image>.

    0 讨论(0)
  • 2021-01-07 03:09

    Hibernate (and JPA in general) persists collections using their interfaces. The list must be declared as a List, and not as a LinkedList. And it won't be loaded with a LinkedList instance, because Hibernate uses its own List implementation to implement dirty-checking, lazy-loading, etc.

    It's a good practice to program on interfaces rather than programming on concrete implementations in general. In JPA entities, it's mandatory.

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