• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic SQL Generation

 
Joe Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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"
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10112
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic