wood burning stoves*
The moose likes Object Relational Mapping and the fly likes Insertion of objects with Sequences + Trigger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Insertion of objects with Sequences + Trigger" Watch "Insertion of objects with Sequences + Trigger" New topic
Author

Insertion of objects with Sequences + Trigger

Lamartine Teixeira
Greenhorn

Joined: Jan 14, 2005
Posts: 2
How can I use Sequence in Id generation if the column also has a trigger associated???

Hibernate first generates a select to get the sequence's next value and use this value in the "insert" statement, but the in my case exists a trigger that increments the id.

The problem is that the sequence value is being incremented twice and hibernate is using the first.

It's a problem because the class where this id is declared has a Collection (one-to-many) and when the children are inserted the parent cannot be found.

P.S.: I can't remove the trigger.

Thanks!!!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Is it really a trigger you've created for the table or a built-in identity column? If a trigger, I'm not sure of the solution. There's an "assigned" ID generator, but the docs say
lets the application to assign an identifier to the object before save() is called.
[my emphasis]
You could, of course, check the Hibernate forums to see if someone has added an "assigned-by-trigger" option.

If it's an identity column, you need to switch the ID generator from "sequence" to "identity":
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL.
This tells Hibernate to let the DB assign the ID for new objects.

It's also possible that "identity" works with a trigger, but I believe that databases that support an identity-type column provide a method to make the last generated ID available that Hibernate uses to store the ID into the object in the Session (and second-level cache, if you're using one). You'd probably have to code up something specific to make the trigger's assigned ID simmilarly available.
[ January 14, 2005: Message edited by: David Harkness ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insertion of objects with Sequences + Trigger
 
Similar Threads
JPA impl. w/Hibernate & Oracle PK Sequences/Triggers
Hibernate, id, oracle, sequence
Trying to save the object value without using Generation Strategy ?
JPA Mapping Exception
why my oracle sequence id increases by 2 when I use hibernate to insert