• 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

Dynamic SQL Generation

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

What is the use of this for the class element in the mapping file,

dynamic-insert="true"
dynamic-update="true"
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so when Hibernate is creating its SessionFactory it generates all the insert, update, and delete statements for all the mapped classes, and makes them prepared statements, so that later when you save and update the statements are already there and Hibernate can be a little bit faster.

So in order to have these statements made up front, it needs to use all the fields, whether you changed them or not.

With dynamic, means that the CRUD statement is created when you go to save, and will only include the fields that changed.

Mark
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
Ok, so when Hibernate is creating its SessionFactory it generates all the insert, update, and delete statements for all the mapped classes, and makes them prepared statements, so that later when you save and update the statements are already there and Hibernate can be a little bit faster.

Mark


First of all thanks very much for all your replies. They are really helpful.

When Hibernate is creating its SessionFactory, it generates the insert, update, and delete as prepared statements. Based on what it generates the update statements?? Can you please help me with an example??
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It just uses all the fields/attributes that you have mapped. If I have an object mapped with id, firstName, lastName, street, address1, city, zip then the update statement will have all those fields.

Do you see what I mean, even if I only changed only firstName the update statement will still include lastName, street, address1, city and zip.

Mark
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
It just uses all the fields/attributes that you have mapped. If I have an object mapped with id, firstName, lastName, street, address1, city, zip then the update statement will have all those fields.

Do you see what I mean, even if I only changed only firstName the update statement will still include lastName, street, address1, city and zip.

Mark



And even if I change the last name, the update statement will include lastname , street, city and zip...and the values for the lastname, street, city and zip will be the current values that are already there in the database for that particular object?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:


And even if I change the last name, the update statement will include lastname , street, city and zip...and the values for the lastname, street, city and zip will be the current values that are already there in the database for that particular object?




Yes, unless you have dynamic-update=true. With that the update will only include the last name.

However, with dynamic-update=true, Hibernate cannot create pre-made PreparedStatements, so there is a little, and I mean little bit of performance hit.

For instances, unless the exact statement has run before in Oracle, Oracle will have to do a cold parse of the query. And Hibernate has to create the statement on the fly, so that is also a slight hit of extra code to have to run.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so the moral of the story.

Unless you have a table that has tons of fields, you don't need to use dynamic-xxx=true in your mappings, let Hibernate pre-generate the PreparedStatements.

Mark
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic