A previous answer explains that a Findbugz/Sonarqube warning Non-transient non-serializable instance field in serializable class
where a serializable class has a member collection should fixed by using the concrete class internally, while using the (non-serializable) interface externally.
1 public class SampleClass implements Serializable {
2
3 private static final long serialVersionUID = 1905162041950251407L;
4
5 private Set<Integer> mySet; // Sonarqube error
6
7 private HashSet<Integer> myOtherSet;
8
9 }
Does that guidance still apply if it the class is a JPA entity which I want to make serializable? Example below ...
1 @Entity
2 @Inheritance
3 @DiscriminatorColumn(name=“fooClass")
4 @Table(name=“foo”, schema = “bar”)
5 public class SampleClass implements Serializable {
6 // SNIP
7 private static final long serialVersionUID = 1905162041950251407L;
8
9 @OneToMany (fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "parent")
10 @MapKey(name = "index")
11 private Map<String, Asset> myMap = new HashMap<>(); // Sonarqube error
12 // SNIP
13 }
EDIT 1
To clarify problem:
- Nearly all the examples I've seen (even if Entity is serializable) use Collection interfaces rather than concrete members, so I wonder is that the 'correct' approach in the 'JPA world'.
- When I change line 11 to
private HashMap<String, Asset> myMap = ...
so as to avoid the FindBugz/Sonarqube warning I see
Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements:
com.foobar.SampleClass.myMap
at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:324) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1723) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:4035) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3989) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1398) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1519) ~[hibernate-entitymanager-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193) ~[hibernate-entitymanager-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100) ~[hibernate-entitymanager-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282) ~[hibernate-entitymanager-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366) ~[hibernate-entitymanager-3.6.10.Final.jar:3.6.10.Final]
... 40
EDIT 2 Now after clarifying issue after being prompted by @Gimby I see at https://stackoverflow.com/a/8169702/449347 that
From the link Hibernate requires that persistent collection-valued fields be declared as an interface type
so I guess this is simply a false-positive from Sonarqube/FindBugz?