-->
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.  [ 6 posts ] 
Author Message
 Post subject: The generated identifier is already in use
PostPosted: Fri Dec 05, 2003 9:00 am 
Newbie

Joined: Tue Nov 11, 2003 3:13 pm
Posts: 10
Hello!

I'm just starting to use hibernate and keep getting this error from one of the entities:

13:36:59,398 ERROR [HibernateEngine] Failed to create job state
13:36:59,398 ERROR [BtExecuteAlertRulesImpl] Failed to set ready state for job 1
com.ikea.ebcopfc01.PersisterException: The generated identifier is already in use: [com.ikea.ebcopfc01.opfc.vo.VoJobState#1] at com.ikea.ebcopfc01.HibernateEngine.createJobState(HibernateEngine.java:391)
at com.ikea.ebcopfc01.opfc.bef.BefJobState.setReadyState(BefJobState.java:29)

I get this error the second time hibernate is trying to create a row with id = 1 even though this row has been deleted by another session (in some other thread) or by myself directly.

The mapping file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.ikea.ebcopfc01.opfc.vo.VoJobState" table="TMP_JOB_STATE_T">
<id column="JOB_ID" name="jobId" type="long">
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>

And the code:

try
{
reconnect();
mSession.save(pJobState);
mSession.flush();
}
catch (Exception e)
{
cCat.error("Failed to create job state");
throw new PersisterException(e);
}
finally
{
disconnect();
}

I never close the session, I just reconnect & disconnect from the underlying datasource. With all other entities this seems to work fine.

Any ideas?

Best regards,
Pelle P


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 10:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Do you ever commit the transaction? If not do so.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 10:30 am 
Newbie

Joined: Tue Nov 11, 2003 3:13 pm
Posts: 10
I'm running this inside websphere which is taking care of my transactions. And the doco tells me that I don't have to commit if I'm using a JTA datasource:

sess.connection().commit(); // not necessary for JTA datasource

Regards,
Pelle P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 3:12 am 
Newbie

Joined: Tue Nov 11, 2003 3:13 pm
Posts: 10
Is it the Hibernate session cache that makes this happen? Hibernate seems to remember that it has used a certain id and refuses to create a new entity with the same identifier, even though the entity with this id has been removed from the db by another session.

This makes me concerned about how Hibernate works in a multiuser/multithreaded environment. If thread A retrieves an entity and thread B updates it, how will thread A know if it's working with cached data? Or as is the case above, thread A adds an entity, thread B removes it, how is A notified if it's using a cache?

This is rather critical for me to understand so I'd appreciate a comment.

Regards,
Pelle P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 3:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the Hibernate manual and look at the code example. The Hibernate Session is transaction-scoped. You should close() it at the end of an "application transaction".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 3:52 am 
Newbie

Joined: Tue Nov 11, 2003 3:13 pm
Posts: 10
I've read the manual several times, apparently without understanding this. I guess it was the following lines in the Session API doco that convinced me that I didn't have to close the session:

public Connection close()
throws HibernateException

End the Session by disconnecting from the JDBC connection and cleaning up. It is not strictly necessary to close() the Session but you must at least disconnect() it.

So, I just disconnect.

Regards,
Pelle P


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.