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: Oracle-generated-primary key
PostPosted: Tue Jun 20, 2006 2:05 pm 
Newbie

Joined: Tue Jun 20, 2006 1:42 pm
Posts: 4
Hi,

I'm working with Oracle and am new to hibernate.
I have a table with an Oracle-generated-primary key. No other columns or column-groups are unique, so I can't use "generator class select".

How can I get the Oracle-generated-primary key in return when saving a new object.
In my object to store, I set id = 0.

Serializable ret = getSession().save(entity);

I tried this: <property name="hibernate.jdbc.use_get_generated_keys">true</property>
but returnvalue stays 0.


Thx,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:53 am 
Newbie

Joined: Wed Apr 19, 2006 12:41 am
Posts: 6
Hi sprinkler,

The following mapping would do this. The sequence is set in the generator element.
Also read the documentation for other options.


<hibernate-mapping>
<class name="myclass" table="myclass">
<id name="entityKey" type="java.lang.Long" column="primarycolumnkey">
<generator class="sequence">
<param name="sequence">DEFINE YOUR ORACLE SEQUENCE NAME HERE</param>
</generator>
</id>
</class>
</hibernate-mapping>

cheers,
Gautam

_________________
Thanks,
Gautam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 3:54 am 
Newbie

Joined: Tue Jun 20, 2006 1:42 pm
Posts: 4
Hi gautamcp,

Thanks for your answer. This seems to work fine, still I have 1 problem with this.

ret = getSession().save(entity);
tx.commit();

In my returnvalue "ret" I get 562 (ex). When I look in my table. The added record has PK 563.
So hibernate uses the Oracle sequence, but doesn"t return the actual PK of the added record?

Any suggestions?

Thx,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 6:05 pm 
Newbie

Joined: Wed Apr 19, 2006 12:41 am
Posts: 6
Hi,

It should return the sequence number of the inserted record. We are using this in our app, and it always works fine. Are you sure record 563 was not inserted manually ? Did you try to debug ?

When you call session.save(..) it will fire the select sequence query and populate your object. Check this value also.

regards,
Gautam

_________________
Thanks,
Gautam


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 2:15 pm 
Newbie

Joined: Tue Jun 20, 2006 1:42 pm
Posts: 4
Hi,

My mistake, Hibernate used the Oracle-sequence, but on my table, I also had a trigger who used this sequence. So double call of the sequence. I disabled the trigger on the table and all works fine now.

Thanks a lot for the help!

Sprinkler


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 7:36 am 
Newbie

Joined: Mon Aug 23, 2004 7:54 am
Posts: 15
I have a trigger too that gets the same value from the sequence.
But I am not allowed to disable it.
How can I use the fact that hibernate.jdbc.use_getGeneratedKeys is set to true?

(JDK1.5, Oracle 10g, Hibernate 3.0)


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.