-->
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: Hibernate Auto Key Generation
PostPosted: Wed Nov 08, 2006 9:52 am 
Newbie

Joined: Wed Nov 08, 2006 9:42 am
Posts: 5
How to generate Primary Key(Data Type int) Auto generation in Oracle using Hibernate Annotation.
In My Sql is working fine using generatedType="AUTO"
But while running Oracle , sequence does not exist error is throwing.
If any body knows about this give me guide line to Auto Generation of Primary key in Oracle.

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 11:18 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Just create the sequence in oracle and everything will be fine!

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject: Hibernate Auto Key Generation
PostPosted: Wed Dec 06, 2006 3:53 am 
Newbie

Joined: Wed Nov 08, 2006 9:42 am
Posts: 5
MikePloed wrote:
Just create the sequence in oracle and everything will be fine!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 4:22 pm 
Newbie

Joined: Wed Dec 06, 2006 4:04 pm
Posts: 8
Location: New Hampshire
I am not using Hibernate to generate my database in Oracle, because there were too many issues (can't specify constraint names, for example) that were a problem to the DBA. So, this is how I'm doing generated ids in Oracle:

1) Specify NUMBER(10) or NUMBER(21) as the datatype in Oracle.
2) Create a SEQUENCE in the database for that table.
3) Write a BEFORE INSERT trigger that references the SEQ.

4) Add the following code before the id getter/setter:

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="CHATITEM_SEQ_GEN")
@SequenceGenerator(name="CHATITEM_SEQ_GEN", sequenceName="CHATITEM_SEQ",initialValue=1,allocationSize=1)
@Column(name = "CHAT_ITEM_ID", insertable= false, updatable = false)

where CHATITEM_SEQ is the name of the SEQEUNCE in Oracle, CHATITEM_SEQ_GEN is the name of the generator in hibernate, and CHAT_ITEM_ID is the name of the column that will have the value generated.

When doing an insert, do not provide a value for the id column, and Oracle will generate one for you.
Hope this helps.
J


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 8:55 pm 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
Um, maybe I'm missing something, but that seems awfully redundant. You either want Hibernate to populate the id (using the Oracle sequence) or you want Oracle to do it in the trigger (something I don't recommend).

And why NUMBER(10) or NUMBER(21)?
NUMBER(8) or, even better, just NUMBER would work just as well.

So:

Create the sequence in Oracle:

create sequence chatitem_seq_gen start with 1 nocycle;

And annotate the id getter as follows:

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="oracleSequence")
@SequenceGenerator(name="oracleSequence", sequenceName="CHATITEM_SEQ",allocationSize=1)
@Column(name="CHAT_ITEM_ID", insertable=false, updatable=false)

To make it more scalable, remove 'allocationSize=1'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 10:48 am 
Newbie

Joined: Wed Dec 06, 2006 4:04 pm
Posts: 8
Location: New Hampshire
Thanks, I will try that.

Regarding number, etc. I am just doing what our DBA says I must... should have mentioned that.

J


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.