-->
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: identity generator and database trigger
PostPosted: Sat Feb 07, 2004 6:29 pm 
Newbie

Joined: Fri Feb 06, 2004 2:44 pm
Posts: 3
Location: Oregon, USA
I am designing an application for a client that uses an Oracle database. The client has a policy that all database inserts use a standard trigger to generate the primary key for a record in every table. The primary key generated by the trigger is a 34 character string.

Is there an identity generator which will insert a new record into a table and then read the record the obtain the primary key value for an id? The sequence generator seems close to what I need, but the hibernate sequence generate returns a long, short, or int.

Thank you,

brad


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2004 4:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Either try using a custom Dialect and override getIdentitySelectString() to return a SQL which fetches the last generated identifier, then use identity generator. Or implement your own custom generator.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2004 4:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
As far as I understand, this problem is quite unsolvable because Oracle provides no way to safely fetch the identifier that was generated by the trigger. You should explain this to your data people and ask them to change their policies.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:09 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
Quote:
As far as I understand, this problem is quite unsolvable because Oracle provides no way to safely fetch the identifier that was generated by the trigger.


Actually you can get the value generated from the trigger using the RETURNING clause on the insert statement:

Code:
  INSERT INTO T1 (C1, C2) VALUES ('V1', 'V2') RETURNING ID INTO hostVariableName;


I don't know which (if any) other database engines support that syntax.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If that works with session.executeQuery("INSERT INTO ... RETURNING ID"), you should be able to include this in your dialect by overriding appendIdentitySelectToInsert() in a custom dialect. Take a look at SybaseDialect for an example.


Top
 Profile  
 
 Post subject: Additional Info
PostPosted: Sat Feb 14, 2004 4:39 pm 
Newbie

Joined: Fri Feb 06, 2004 2:44 pm
Posts: 3
Location: Oregon, USA
Thanks for the tips on using the returning clause. I will look into it. I found some additional and useful information at ExpertsExchange in this thread:

http://www.experts-exchange.com/Databas ... ml#8893641

FYI. Registration required to see the full thread.

Thanks,

Brad


Top
 Profile  
 
 Post subject: CallableStatement Needed for Implementation of Returning
PostPosted: Sun Feb 15, 2004 3:13 pm 
Newbie

Joined: Fri Feb 06, 2004 2:44 pm
Posts: 3
Location: Oregon, USA
Based on the information in the thread cited above (experts exchange), the Returning clause in Oracle's sql has to be accessed via a stored procedure and a CallableStatement from jdbc.

Is this possible with Hibernate? Or will I need to implement custom versions of the ClassPersister interface as well as a custom dialect?

Thanks,

Brad


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.