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.
posted 11 years ago
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 ]