I am frequently having an error when merging a detached object outside of a transaction. (Is this allowed? I am not trying to modify the object, just read data from it.)
Here is a test case:
package com.experiments; import javax.persistence.*; import java.util.HashMap; public class ErrorTest { public static void main(String[] args) throws Exception { HashMap<String, String> map = new HashMap<String, String>(); map.put("javax.persistence.jdbc.user", "xxxxx"); map.put("javax.persistence.jdbc.password", "xxxxxx"); final EntityManagerFactory emf = Persistence.createEntityManagerFactory("objectdb://xxxxxx/test.odb", map); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); A a = new A("first version"); em.persist(a); em.getTransaction().commit(); em.close(); em = emf.createEntityManager(); em.getTransaction().begin(); em.merge(a).setValue("second version"); em.getTransaction().commit(); em.close(); em = emf.createEntityManager(); a = em.merge(a); System.out.println(a.getValue()); em.close(); } @Entity public static class A { @Id @GeneratedValue Long id; String value; A(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }
Here is the error:
Exception in thread "main" [ObjectDB 2.0.5_03] Unexpected exception (Error 990) Generated by Java HotSpot(TM) Client VM 1.6.0_18 (on Windows 7 6.1). Please report this error on http://www.objectdb.com/database/issue/new com.objectdb.o.InternalException: com.objectdb.o._IllegalArgumentException: Attempt to modify an object with no active transaction when NontransactionalWrite is disabled: com.fastmodel.fastplan.db.experiments.ErrorTest$A#64 [ObjectDB 2.0.5_03] java.lang.IllegalArgumentException Attempt to modify an object with no active transaction when NontransactionalWrite is disabled: com.fastmodel.fastplan.db.experiments.ErrorTest$A#64 (error 635) at com.objectdb.jpa.EMImpl.merge(EMImpl.java:428) at com.experiments.ErrorTest.main(ErrorTest.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
ObjectDB always fails for this use case. I can make the code work by beginning a transaction in the last step. But I think this should be allowed. Let me know. Thanks.
Carl