• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to persist only the value which changed?

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
Mac Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic