Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to persist only the value which changed?

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks for any help.
 
Logan Lee
Greenhorn
Posts: 18
Java Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Hibernate Session docs
EntityManager API
 
Vijay Oruganty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help -

"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."
 
afsar khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic