-->
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.  [ 12 posts ] 
Author Message
 Post subject: DB2 Identity ID Generation
PostPosted: Sun Sep 07, 2003 3:36 pm 
Newbie

Joined: Sun Sep 07, 2003 3:01 pm
Posts: 5
Hello,

I am very new to hibernate and am having a problem with using the identity generator with DB2 V7.2. After creating a new object and passing it to saveOrUpdate I believe hibernate is supposed to save the new id value for the newly created row in the database in that property. This does not occur however, it just sets it to zero. I believe it is having problems retrieving the Identity value for the new entry. From the debug output below it appears to call some function IDENTITY_VAL_LOCAL to get the identity value but that just returns 0. The last line is a call to getID after saveorupdate has been called from the driving program. I have changed it over to use a sequence instead and it is now working. Has anyone else encountered the same problem or know what I might be doing wrong ? Thanks in advance for your help!

Thanks,
Eric


--Debug output --
14:07:02,160 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
14:07:02,160 DEBUG [SessionFactoryImpl] prepared statement get: insert into BaseDataObject (type, id) values (?, default)
14:07:02,160 DEBUG [SessionFactoryImpl] preparing statement
14:07:02,160 DEBUG [StringType] binding 'HelloWorld1' to parameter: 1
14:07:02,160 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
14:07:02,160 DEBUG [SessionFactoryImpl] closing statement
14:07:02,160 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
14:07:02,160 DEBUG [SessionFactoryImpl] prepared statement get: values IDENTITY_VAL_LOCAL()
14:07:02,160 DEBUG [SessionFactoryImpl] preparing statement
14:07:02,160 DEBUG [NormalizedEntityPersister] Natively generated identity: 0
14:07:02,160 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
14:07:02,160 DEBUG [SessionFactoryImpl] closing statement
14:07:02,160 DEBUG [Cascades] processing cascades for: edu.jhu.ccbm.pathway.data.BaseDataObject
14:07:02,175 DEBUG [Cascades] done processing cascades for: edu.jhu.ccbm.pathway.data.BaseDataObject
14:07:02,175 DEBUG [HibernateTestServlet] 0


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Looks fine to me. Zero is probably the first available id!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:17 pm 
Newbie

Joined: Sun Sep 07, 2003 3:01 pm
Posts: 5
This was generated after having made a couple of inserts into the database through a sql script to initalize the db. I am pretty certain this is not the first value. If I later try to add a related item I get a FK error because the inserted id is not a valid id in the related table. I was also having issues with using the sequence mapping for some reason it looked like the inserts were happening but nothign was persisted to the database. I noticed this only happens if I open up a session do a saveorUPdate then a find and then a saveorupdate within the same session. In this situation the second saveorupdate generates debugging output but nothing is persisted to the database, if I remove the find inbetween then it works fine. In the context of a session I thought I should be able to do some inserts then some queries then some more inserts, if this is not the correct use of a session then I will go change my code. I've since moved things to mysql and it seems to work well (with identity columns), but am willing to switch back to DB2 and get more output or anything. Thanks in advance for all of your help.

Thanks,
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Are you sure you have "GENETRATED BY DEFAULT AS IDENTITY" in your schema definition for the primary key?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:41 pm 
Newbie

Joined: Sun Sep 07, 2003 3:01 pm
Posts: 5
Yes I am pretty sure. Like if I look at the actual database the IDs are generated correctly. Also if I do an saveorupdate to add a new object and then query for it and call getID on it it will return the correct value. But if I do a getID after calling saveorupdate it will return 0. Thanks for your help!


Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Do you have some kind of trigger on the table?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:52 pm 
Newbie

Joined: Sun Sep 07, 2003 3:01 pm
Posts: 5
Nope I just ran the SQL that came out from the hibernate schema generator to create the database.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
well, i dunno. ask your db2 expert.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hey! Are you 100% certain that autocommit is disabled!?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:24 am 
Newbie

Joined: Sun Sep 07, 2003 3:01 pm
Posts: 5
Galvin, thanks for your help. This might be the problem, I have it deployed as a SAR in jboss using a connection pool that I created as a speerate resource in jboss. I believe autocommit is automatically turned on when getting a connection this way, although I am not sure. I will try to investigate this more tomorrow, although it seems to work ok with mysql. Thanks again for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I imagine that the difference is that the equivant MySQL function call is scoped to a connection, whereas the DB2 call is scoped to a transaction. This makes sense since MySQL is nontransactional, be default.


Top
 Profile  
 
 Post subject: Answer from IBM KB.
PostPosted: Thu Sep 25, 2003 6:40 pm 
Regular
Regular

Joined: Fri Sep 12, 2003 12:40 pm
Posts: 65
ewchen wrote:
This was generated after having made a couple of inserts into the database through a sql script to initalize the db.


I've been researching this problem and I believe it is caused by:

Correct use of identity

So, you either need to use the word 'default' in your scripts (as Hibernate correctly does), or you need to execute:

ALTER TABLE <table> ALTER COLUMN <id col> RESTART WITH <nextval>

after your inserts.

I hope this helps you.

_________________
- Brian


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