具有集合成员的可序列化JPA实体

时间:2015-10-02 09:07:03

标签: java jpa serialization sonarqube

之前的answer解释了可序列化类具有成员集合的Findbugz / Sonarqube警告Non-transient non-serializable instance field in serializable class应该通过内部使用具体类来修复,同时在外部使用(非可序列化)接口。

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 }

如果该类是我想要序列化的JPA实体,那么该指南是否仍然适用?以下示例......

 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 }

编辑1

澄清问题:

  1. 几乎所有我见过的例子(即使实体是可序列化的) 使用Collection接口而不是具体成员,所以我想知道&#39;正确&#39;在&#39; JPA世界中的方法&#39;。
  2. 当我将第11行更改为private HashMap<String, Asset> myMap = ...
  3. 以避免我看到的FindBugz / Sonarqube警告

    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 
    

    编辑2 现在在@Gimby提示之后澄清问题后我明白了 https://stackoverflow.com/a/8169702/449347

      

    从链接Hibernate要求持久集合值   字段被声明为接口类型

    所以我猜这只是来自Sonarqube / FindBugz的假阳性?

1 个答案:

答案 0 :(得分:1)

如上面问题EDIT 2中所述,Hibernate 3.6 JPA实现要求将持久的集合值字段声明为接口类型。

因此,这是静态代码分析工具的误报。