2

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:

  1. 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'.
  2. 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?

Community
  • 1
  • 1
k1eran
  • 4,492
  • 8
  • 50
  • 73
  • I wouldn't refer to warnings as false-positives. Warnings are just that: warnings. A sign that something has a reasonable possibility of causing issues, but not with guarantee. – Gimby Oct 02 '15 at 11:08

1 Answers1

1

As described in EDIT 2 in above question, the Hibernate 3.6 JPA implementation requires that persistent collection-valued fields be declared as an interface type.

So this is a false-positive from the static-code analysis tools.

k1eran
  • 4,492
  • 8
  • 50
  • 73