Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

ConcurrentModificationException with self-referring entity-class
when not running the enhancer

morten bendiksen

2008-06-20


Author LoginPost Reply
Sometimes while fetching an instance of an entity-class with a self-reference (ManyToOne , for creating a hierarchy) , I get a ConcurrentModificationException. This only seems to happen when the enhancer hasn't been loaded. Also it seems there has to be multiple levels in the hierarchy for this to occur.

I have created an example which reproduces the Exception.

The code:

@Entity
public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @ManyToOne
    @Column(name = "parent")
    private A parent;

    public A() {
          name = "";
    }

    public A(String name) {
          this.name = name;
    }

    public static void main(String[] args) {
          EntityManagerFactory emf = Persistence
                    .createEntityManagerFactory("system");

          EntityManager em = emf.createEntityManager();

          em.getTransaction().begin();

          // a
          A a = new A("a");
          em.persist(a);

          // b
          A b = new A("b");
          b.setParent(a);
          em.persist(b);

          // c
          A c = new A("c");
          c.setParent(b);
          em.persist(c);

          em.getTransaction().commit();
          em.close();


          // getting c's data
          int cId = c.getId();
          String cName = c.getName();


          em = emf.createEntityManager();
          em.getTransaction().begin();

          //both methods of getting entity results in same error
          // A newC = em.find(A.class, cId);
          Query q = em.createQuery("SELECT a FROM A a WHERE a.name=:cName")
                    .setParameter(1, cName);

          A newC = (A)q.getSingleResult();

          em.getTransaction().commit();
          em.close();

          System.out.println(newC);

          emf.close();
    }

    //getters and setters

}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="1.0">

  <persistence-unit name="system" transaction-type="RESOURCE_LOCAL">

          <class>no.tecwel.A</class>

    <properties>
          <!-- this has no effect
               <property name="openjpa.Multithreaded"
          value="true"/> -->

       <property name="openjpa.ConnectionURL"
          value="jdbc:mysql://db.home.local/system "/>
       <property name="openjpa.ConnectionDriverName"
          value="com.mysql.jdbc.Driver"/>
       <property name="openjpa.ConnectionUserName"
          value="system"/>
       <property name="openjpa.ConnectionPassword"
          value="pass"/>

      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add,deleteTableContents',ForeignKeys='true')"/>

       <property name="openjpa.Log" value="DefaultLevel=TRACE"/>

    </properties>
  </persistence-unit>

</persistence>

Best regards
Morten Bendiksen
Tecwel AS



The information contained in this message is sent in the strictest confidence for the addressee only. It is intended only for the use of the addressee and may contain legally privileged information. If you have received this e-mail in error you are requested to preserve its confidentiality and advise the sender of the error in transmission.
It is the responsibility of the addressee to scan this email and any attachments for computer viruses or other defects. The sender does not accept liability for any loss or damage of any nature, however caused, which may result directly or indirectly from this email or any file attached.
All mail from TecWel AS are scanned by both hardware and software antispam and antivirus filters.
©2008 gg3721.com - Jax Systems, LLC, U.S.A.