本文介绍了Hibernate用一个嵌入的ElementCollection抛出ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Hibernate 4.2.6.Final / JPA2并尝试拥有一个@ElementCollection列表@Embeddable对象,它们本身包含@ElementCollection字符串列表。





如果我注释掉用户中列出< Address> ,一切正常运行。同样,如果我注释掉 List< String>街道位于地址中,所有内容都正常启动。我假设它有一个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)
私人长ID;

/ **
*登录用户名
* /
@NotNull
私人字符串用户名;

/ **
*加密格式的密码
* /
私人字符串密码;
/ **
*地址
* /
@Valid
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name =user_address ,joinColumns = @ JoinColumn(name =user_id,referencedColumnName =id))
@OrderColumn $ b $ private List< Address>地址;

//为简洁省略getters和setter
}


@Embeddable
公共类地址{

/ **
*允许使用多条街道
* /
@NotBlank
@ElementCollection(targetClass = String.class,fetch = FetchType.LAZY)
@CollectionTable (joinColumns = @ JoinColumn(name =address_id,referencedColumnName =id))
@OrderColumn
private List< String>街;

/ **
*城市
* /
私人字符串城市;

/ **
*州/省
* /
私人字符串状态;

/ **
*国家
* /
私人字符串国家;

//为了简洁而省略了getters和setter
}


由java.util.ConcurrentModificationException引发的java.util中的
。 ArrayList $ Itr.checkForComodification(ArrayList.java:859)$ b $在java.util.ArrayList $ Itr.remove(ArrayList.java:845)
在org.hibernate.cfg.Configuration.originalSecondPassCompile(配置。
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1390)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1777)
(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.springframewo rk.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
处org.springframework.beans org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 40 more

我不允许嵌入类拥有自己的 @ElementCollection ?这是不允许的?我该如何做到这一点呢?

我解决了一些问题,发现这个,反过来引用这个Jira问题:





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



目前的解决方法是使Address成为实体,但通过 User 保持生命周期。


I'm running Hibernate 4.2.6.Final / JPA2 and trying to have an @ElementCollection List of @Embeddable objects which themselves contain an @ElementCollection List of String.

However, hibernate is throwing a ConcurrentModificationException when trying to instantiate the EntityManager, which I do not understand at all.

If I comment out my List<Address> in User, everything runs properly. Similarly, if I comment out the List<String> street in Address, everything launches properly as well. I am presuming it has something to do with an ElementCollection having its own embedded 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

Am I not allowed having an embedded class have its own @ElementCollection? Is this not allowed? How do I do this otherwise?

解决方案

I spent some time digging around a little more and found this similar issue on SO which in turn references this Jira issue:

ConcurrentModificationException when collection of embeddable contains a collection

It was already raised in 2007, but to date nothing has been implemented for it.

Workaround at the moment is to make Address into an Entity, but maintain the lifecycle through User.

这篇关于Hibernate用一个嵌入的ElementCollection抛出ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:25