-->
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: Optimistic Locking issue with <version> tag
PostPosted: Mon Jul 09, 2007 11:37 am 
Newbie

Joined: Fri Jul 06, 2007 2:28 am
Posts: 2
Location: Kochi, Kerala, India
Hi friends,

I was trying to implement optimistic locking in my APIs where hibernate is the persistence layer. As per the hibernate documents, I have done the version configuration, by adding the <version> tag in the hibernate mapping files. But while running the application, it is showing the following exception, whenever I tried to save a new object to datastore using session.save(object) call.

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.mypackage.Bid

Hibernate version:
Hibernate 3

Mapping documents:
1) Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.mypackage.Item" table="item" >
<comment></comment>
<id name="PRIMARY_KEY" type="int">
<column name="PRIMARY_KEY" />
<generator class="org.mypackage.MyHibernateIDGenerator" />
</id>
<version name="VERSION" column="VERSION" type="java.lang.Integer" />
<many-to-one name="bid" class="org.mypackage.Bid" fetch="select">
<column name="BID_ID">
<comment></comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>

2) Bid.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.mypackage.Bid" table="bid" >
<comment></comment>
<id name="PRIMARY_KEY" type="int">
<column name="PRIMARY_KEY" />
<generator class="org.mypackage.MyHibernateIDGenerator" />
</id>
<version name="VERSION" column="VERSION" type="java.lang.Integer" />
<set name="items" inverse="true">
<key>
<column name="bid_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="org.mypackage.Item" />
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
I am using CMP framework.
try
{
Session session = getHibernateSession();
Item i = new Item();
i.setBid( new Bid (bidId) );
session.save(i);
} catch (Exception e){
e.printStackTrace();
}

Full stack trace of any exception that occurs:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.mypackage.Bid
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:951)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:109)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:88)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1540)

Name and version of the database you are using:
MySql 5.0.27

The generated SQL (show_sql=true):
No sql generated.

Debug level Hibernate log excerpt:
-

Any helps are most welcome. And thanks in advance.
Manoj.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 2:11 pm 
Newbie

Joined: Mon Jul 02, 2007 2:12 pm
Posts: 18
Hi Manoj,

try adding cascade="all" to your many-to-one mapping.

See http://www.hibernate.org/hib_docs/v3/re ... d-cascades

-Thomas


Top
 Profile  
 
 Post subject: That worked
PostPosted: Mon Jul 09, 2007 11:39 pm 
Newbie

Joined: Fri Jul 06, 2007 2:28 am
Posts: 2
Location: Kochi, Kerala, India
Hello Thomas,

You are absolutely right, and it worked fine. Thanks alot for your helpful reply.

Manoj.


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.