File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Insertion of objects with Sequences + Trigger" Watch "Insertion of objects with Sequences + Trigger" New topic

Insertion of objects with Sequences + Trigger

Lamartine Teixeira

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.

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:
subject: Insertion of objects with Sequences + Trigger
jQuery in Action, 3rd edition