-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: persist() doesn't work for assigned identifiers ?
PostPosted: Fri Sep 22, 2006 10:49 am 
Newbie

Joined: Fri Sep 22, 2006 10:36 am
Posts: 1
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)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 8:48 am 
Newbie

Joined: Thu Oct 19, 2006 9:11 am
Posts: 3
Hi,

I'm having the same problem, although I'm not sure what exactly it is in my entities that is causing the PersistentObjectException (detached entity passed to persist).

Did you find a solution, or possibly some documentation somewhere that helps to explain this, and how to get around it?

Thanks very much in advance for your reply,

Adam Crowther.


Top
 Profile  
 
 Post subject: Persist() error for assigned key
PostPosted: Wed Jan 03, 2007 9:16 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
I too have the same issue with objects that have assigned key .But it works fine if the key is auto generated.So in my dao's for those objects which are using assigned generator Im using saveOrUpdate to persist .
FYI-
Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.