-->
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.  [ 7 posts ] 
Author Message
 Post subject: Session.save() not returning true primary key value.
PostPosted: Tue May 08, 2007 5:16 pm 
Newbie

Joined: Tue May 08, 2007 5:09 pm
Posts: 4
Hi friends,

In our database whenever we add a new record using session.save() it returns a wrong primary key. After successful insertion of record in Database actual primary key assigned to the record is different form the one which is returned by session.save(Object) method call. On addition to that to our surprize database table doesn't have any record for the value which is returned by session.save().

We are using sequence generator for generating ID.

Please share your views on how to tackel this problem.

Thanks & Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 9:33 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi yatrik,
how have you mapped your id in your mapping file? can u post your .hbm.xml mapping file?

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 9:54 am 
Newbie

Joined: Tue May 08, 2007 5:09 pm
Posts: 4
Hi Chucky,

Thanks for the interest in my problem.

Here is my mapping file.

<?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 package="com.ris.estimator.dao">

<class name="com.beans.Payment" table="CAR_PYMT" dynamic-update="true" lazy="true">

<id name="PaymentId" column="PYMT_ID">
<generator class="sequence">
<param name="sequence">CAR_PYMT_Q1</param>
</generator>
</id>

<property name="waybillId" column="WB_ID"/>
<many-to-one name="HireEventId" column="HIRE_EVNT_ID" class="com.beans.HireEvent" not-null="false" lazy="true"/>
<many-to-one name="AdjustmentId" column="ADJ_ID" class="com.beans.Adjustments" not-null="false" lazy="true"/>
<property name="equipmentInitial" column="EQMT_INIT"/>

...........
...........
other properties.
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 10:00 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi yatrik,
if you want to assign a primary key, change the generator class. Use
Code:
<generator class="assigned"/>

instead of
Code:
<generator class="sequence">
<param name="sequence">CAR_PYMT_Q1</param>

or hibernate will generate an id for you.

Hope that helps

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 10:06 am 
Newbie

Joined: Tue May 08, 2007 5:09 pm
Posts: 4
Hi chucky,

Yah that could be an option but as per our DB design they want oracle sequence to generate Id and assign it. so at run time I should be able to get the Id which is assigned to my recently added record as a return value of Session.save(obj) method. But somehow this return value is not same as the actual value which is being assiged as primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 11:23 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi yatrik,
if hibernate should assign the id via the sequence, you should get the right sequence number. Unless there's an external process that has also access to the same sequence. What is the code that is performing this? The full stack trace would also be helpful.
For more information about what hibernate is doing, it would be better to set the debug level to debug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 4:43 pm 
Newbie

Joined: Tue May 08, 2007 5:09 pm
Posts: 4
Hi chucky,

I think I found the problem and that was not with Hibernate but with our DB design. In DB Sequences were set with auto increment so when ever Hibernate tries to insert a record and supply the primary key generated using sequence.nextval my database table would ignore that value and generate its own primary key using sequence.nextval.

that is why actual primary key is diffenent from primary key which was supplied by Hibernate. Hibernate will return the value which was passed by it as primary key to the caling function but actually this value is not used and ignoredby DB table.

Thanks for your help.

Yatrik.


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