Hi all,
If I have a large table with many columns and so my corresponding POJO is also large. Now supposing that in my application I only changed one field in this POJO (one column in the table) and I do not want to persist the entire POJO (for obvious performance reasons) then which method in the Hibernate API do I need to call? Is it merge or update or something else?
In other words how can I ensure that Hibernate will only update the one column which changed rather than trying to persist the entire object?
Jehan Jaleel wrote:
In other words how can I ensure that Hibernate will only update the one column which changed rather than trying to persist the entire object?
You can use the merge method to synchronize detached entities with your persitence provider. You can only use the persist method on object instances that are the new state.
edit:
save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE.
"So, by default, Hibernate includes all columns in the SQL
UPDATE statement (hence, Hibernate can generate this basic SQL at startup, not at
runtime). If you only want to update modified columns, you can enable dynamic
SQL generation by setting dynamic-update="true" in a class mapping."
If you are sure about which column of the table you actually wants to update in advance then you can create an update template in hbm file and then by calling that template through getNameQuery will help you in updating on that particular column only.
I hope this will help you.....
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Gift giving made easy with the permaculture playing cards