Hibernate使用嵌入的ElementCollection抛出ConcurrentModificationException

时间:2013-12-03 19:09:07

标签: java hibernate jpa

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

但是,在尝试实例化EntityManager时,hibernate会抛出一个ConcurrentModificationException,我完全不理解。

如果我在List<Address>中注释掉我的User,那么一切都正常运行。同样,如果我在List<String> street中注释掉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吗?这是不允许的?我怎么办呢?

2 个答案:

答案 0 :(得分:2)

这个已经向Hibernate开发人员报告的问题并没有安排修复!抱歉。请参阅https://hibernate.atlassian.net/browse/HHH-4313

答案 1 :(得分:2)

我花了一些时间再挖掘一下,发现这个similar issue on SO反过来引用了这个Jira问题:

ConcurrentModificationException when collection of embeddable contains a collection

它已于2007年提出,但迄今为止尚未实施任何内容。

目前的解决方法是将地址变为Entity,但通过User维护生命周期。