• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Insertion of objects with Sequences + Trigger

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic