File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Dynamic SQL Generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Dynamic SQL Generation" Watch "Dynamic SQL Generation" New topic
Author

Dynamic SQL Generation

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Guys,

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

dynamic-insert="true"
dynamic-update="true"


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Sep 26, 2006
Posts: 9243
    
    1

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Sep 26, 2006
Posts: 9243
    
    1

Thanks Mark!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic SQL Generation
 
Similar Threads
clarification reg jsp:invoke, variable directive etc..
Dynamic attribute
mock question regarding dynamic attribute
Html struts tags attribuits
WA #1.....word association