DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Java
  4. Hibernate hard facts – Part 7

Hibernate hard facts – Part 7

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Jun. 07, 11 · Interview
Like (0)
Save
Tweet
Share
8.89K Views

Join the DZone community and get the full member experience.

Join For Free

In the seventh article of this serie, we’ll have a look at the difference between saveOrUpdate() and merge().

  • Part 1: Updating a persistent object
  • Part 2: Directional associations
  • Part 3: Custom type mapping
  • Part 4: Choosing between get() and load()
  • Part 5: Manage logical DELETE
  • Part 6: Fetch profiles

Update

Hibernate’s way for updating entities is through the update() method. We can get an object, detach it from the session and then update it later with no problem. The following snippet shows how it works:

Session session = factory.getCurrentSession();

Customer customer = (Customer) session.get(Customer.class, 1L);

session.getTransaction().commit();

customer.setFirstName("John");

beginTransaction();

session = factory.getCurrentSession();

session.update(customer);

This will issue a SQL’s UPDATE statement.

However, update()‘s Javadoc states that “If there is a persistent instance with the same identifier, an exception is thrown”.

This means that loading the same customer just before the update (and after the transaction’s beginning) will miserably fail – with a NonUniqueObjectException.

Session session = factory.getCurrentSession();

Customer customer = (Customer) session.get(Customer.class, 1L);

session.getTransaction().commit();

customer.setFirstName("John");

beginTransaction();

session = factory.getCurrentSession();

session.get(Customer.class, 1L);

// Fail here

session.update(customer);

Merge

Merging is a feature brought by Hibernate 3, inspired by JPA. Whereas update conflicts if the same entity is already managed by Hibernate, merge also copies the changes made to the detached entity so there’s no exception thrown.

Session session = factory.getCurrentSession();

Customer customer = (Customer) session.get(Customer.class, 1L);

session.getTransaction().commit();

customer.setFirstName("John");

beginTransaction();

session = factory.getCurrentSession();

session.get(Customer.class, 1L);

// Succeeds!

session.merge(customer);

As opposed to update, note that merge doesn’t associate the detached instance to Hibernate’s memory store but returns a new instance that’s in it instead. Further operations should be made to the returned instance.

Conclusion

When in a web context, detached entities are everywhere.  Most of the time, update() should be enough to do the trick. When not, think about merge() but keep in mind to use the returned instance.

Finally, if the entity cannot be reconstructed – because some attributes are missing from the web page – the best strategy is to load the entity from the database and then modify its attributes as needed. In this case, there’s no need to update nor merge: the statement will be executed during session flush.

You can download sources for this article here in Maven/Eclipse format.

 

From http://blog.frankel.ch/hibernate-hard-facts-%E2%80%93-part-7

Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Reliability Is Slowing You Down
  • How To Handle Secrets in Docker
  • Cloud Performance Engineering
  • Testing Repository Adapters With Hexagonal Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: