I have a class which I recently changed from native to assigned identifiers.
After doing so I began getting the following exception when persisting new instances
org.hibernate.PersistentObjectException: detached entity passed to persist:
The objects being passed to persist are newly created, never associated with a session. I've recreated this with the simple test class below.
I discovered that "merge" works, it generates an insert statement, but I find it strange since this is a newly created object, not previously managed. From the documentation I would expect that persist is the proper method for a new object.
Is there an explanation for this, or is this a bug in the implementation? I have read the JSR 220 section on persist. What am I missing?
public void testPersist() throws Exception {
TestPersist obj = new TestPersist();
obj.setId(1);
HibernateUtil.getSession().beginTransaction();
HibernateUtil.getSession().persist(obj); // <== throws exception
// HibernateUtil.getSession().merge(obj); // <== works
HibernateUtil.getSession().getTransaction().commit();
}
public class TestPersist {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
<hibernate-mapping package="org.genepattern.server.domain">
<class name="TestPersist" table="TEST_PERSIST" optimistic-lock="none">
<id name="id" type="integer" unsaved-value="null">
<column name="ID" not-null="true" unique="true" precision="10" scale="0"/>
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>
org.hibernate.PersistentObjectException: detached entity passed to persist: org.genepattern.server.domain.TestPersist
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:613)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:587)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:591)
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:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.persist(Unknown Source)
at org.genepattern.server.domain.TestPersistTest.testPersist(TestPersistTest.java:44)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
|