小编典典

Hibernate抛出带有嵌入式ElementCollection的ConcurrentModificationException

java

我正在运行Hibernate 4.2.6.Final /
JPA2,并尝试具有@Embeddable对象的@ElementCollection列表,这些对象本身包含String的@ElementCollection列表。

但是,hibernate在尝试实例化EntityManager时抛出ConcurrentModificationException,我根本不了解。

如果我注释掉List<Address>in User,则一切运行正常。同样,如果我注释掉List<String> streetin
Address,那么所有内容也会正常启动。我假设它与具有自己的嵌入式ElementCollection的ElementCollection有关。

public class User {

    @TableGenerator( name="UUIDGenerator", pkColumnValue="user_id", table="uuid_generator", allocationSize=1)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator="UUIDGenerator")
    @Column(name = "id")
    private Long id;

    /**
     * Login username
     */
    @NotNull
    private String username;

    /**
     * Password in encrypted format
     */
    private String password;
    /**
     * Address
     */
    @Valid
    @ElementCollection(fetch=FetchType.LAZY)
    @CollectionTable(name="user_address", joinColumns=@JoinColumn(name = "user_id", referencedColumnName = "id"))
    @OrderColumn
    private List<Address> address;

   // getters and setters omitted for brevity
}


@Embeddable
public class Address {

    /**
     * Multiple street lines allowable
     */
    @NotBlank
    @ElementCollection(targetClass=String.class, fetch=FetchType.LAZY)
    @CollectionTable( joinColumns=@JoinColumn(name = "address_id", referencedColumnName = "id"))
    @OrderColumn
    private List<String> street;

    /**
     * City
     */
    private String city;

    /**
     * State/Province
     */
    private String state;

    /**
     * Country
     */
    private String country;

    // getters and setters omitted for brevity
}


Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.remove(ArrayList.java:845)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1633)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1390)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1777)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 40 more

我是否不允许嵌入式班级有自己的班级@ElementCollection?这是不允许的吗?否则我该怎么做?


阅读 288

收藏
2020-11-26

共1个答案

小编典典

我花了一些时间进行深入研究,并在SO上发现了类似的问题,该问题又引用了此Jira问题:

ConcurrentModificationException当可嵌入的集合包含一个集合时

它已经在2007年提出,但是迄今为止还没有实现。

目前的解决方法是使Address成为一个Entity,但保持生命周期User

2020-11-26