• 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

Hibernate: Optimization: id generation & two step insert

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

Question on optimizing hibernate's SQL generation when inserting into a table using "sequence" generation of the primary key. Basically what I've noticed is that when hibernate generates the SQL for an insert it is doing so in two steps.

1) Select NEXTVAL FOR SQ_MySequence FROM SYSIBM.dummy1
2) INSERT using the value from step 1 as the primary key

Obviously it would be better if hibernate generated a single SQL statement that looked something like this
INSERT INTO MyTable (id, arg1) VALUES (NEXTVAL FOR SQ_MySequence, val1)

Any ideas on how I'd change my mapping to cause this?

Here's my current mapping:

Thanks,
D
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) Select NEXTVAL FOR SQ_MySequence FROM SYSIBM.dummy1
2) INSERT using the value from step 1 as the primary key

Obviously it would be better if hibernate generated a single SQL statement that looked something like this
INSERT INTO MyTable (id, arg1) VALUES (NEXTVAL FOR SQ_MySequence, val1)


Does this really need optimisation? The single SQL statement is still two statements, albeit it the result no longer has to go back over the network. Do you have a performance issue in your application with this?

If you do you can't change the behaviour via the mapping. What you will need to do is implement your own dialect and generator classes that do this work for you.
 
Doug MacKenzie
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

You're right, it's not a HUGE optimization, I just found hibernate's generated SQL to be "messy" and causes un-nessary code, when it needn't be. I'm sure I'll come across other instances as I continue developing with hibernate so I was curious as to how I could optimize the mapping. Thanks for the solution

D
 
reply
    Bookmark Topic Watch Topic
  • New Topic