Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which generator type to use..?

 
Jigar Gohil
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello All,
I have a situation in which I am having a database with one sequence & trigger per table. The trigger simply sets the Primary key of a table before inserting any record using the sequence created for that table.
Using sequence as generator type doesn't work coz of the triggers i have placed. The sequence gets incremented twice

I want that primary key to be returned while saving the object.

can anyone suggest which generator type can i use..?

Thanks in advance.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could just use an assigned generator. But then again, is there an issue with the sequence getting incremented twice? Do you still have a not null, unchanging, unique value for your primary key?


 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it really sounds messy, because your sequence generator is your custom-built trigger. So, typically, either we generate our ids ourselves, or we allow a sequence or id generator on the database do it. With your solution, you're sorta blazing new ground, and it doesn't seem to fit into any of the standard strategies.

Jigar, I edited your subject line to remove the 'urgent' request. Here's what we find when posts are marked urgent:

JavaRanch Experience with URGENT Request

Good luck!
 
Jigar Gohil
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank You Paul and Cameron...
First of all the reason for using triggers for id generation is... m having no. of procedures which inserts data into the tables.. its not only the application through which data is getting inserted. So, I wanted the sequences to be shared between application code and back-end procedures.

n e ways...I have applied one solution which i could think of...i.e
I have used sequence as my generator type & modified my triggers with one condition as follows:




Its simple with hibernate insert query...already there will be an id value... so no need to use the sequence again....


Any one with better solution than this would be appreciated...

Thank You, CodeRanch.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic