What are the differences between the different saving methods in Hibernate?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Hibernate, a popular object-relational mapping (ORM) framework for Java, provides several methods for saving objects to a database. Understanding these methods is crucial for designing efficient and effective persistence layers. This article explores the differences between save(), persist(), saveOrUpdate(), and merge() methods, providing technical explanations and examples. By doing so, developers can choose the most suitable method for their specific needs.
Key Saving Methods in Hibernate
save()
save() is a method provided by Hibernate's Session interface. It is used to save an entity object into the database. This method returns the generated identifier, if any, after saving.
Key Characteristics:
- Used to save a transient object to the database.
- Generates an identifier and persists the object immediately.
- Throws a
HibernateExceptionif invoked outside of a transaction boundary. - Returns the generated identifier (Serializable).
Example:
persist()
The persist() method is part of both JPA's EntityManager and Hibernate's Session interface in the context of Hibernate. It is more aligned with the lifecycle management of an entity.
Key Characteristics:
- Used to make a transient instance persistent.
- Does not return the primary key identifier.
- Executes within the transaction boundaries but doesn't guarantee immediate insertion.
- Adheres to JPA specifications.
Example:
saveOrUpdate()
The saveOrUpdate() method can handle both new and existing entities. It determines if an entity is new or existing based on the identifier value.
Key Characteristics:
- If an object doesn't have an identifier value, it calls
save(). - If an object with an identifier already exists, it calls
update(). - Useful for batch operations or when the persistence state is not tracked.
Example:
merge()
The merge() method is used to save or update an entity object in a detached state. It merges the changes from a detached object into the active session context.
Key Characteristics:
- Handles detached entity instances and can update them with the current session.
- Returns the managed instance with the updated state.
- Preferable when dealing with objects received from external layers (e.g., DTOs).
Example:
Comparison Table
| Method | Use Case | Return Type | Transaction Context | Insert/Update Strategy |
save() | Save a new entity | Serializable | Required | Immediate insert |
persist() | Manage entity lifecycle | void | Required | Integration with JPA specification Deferred until flush commit |
saveOrUpdate() | Save new or update existing entity | void | Required | Save for no identifier Update if identifier exists |
merge() | Update detached entity | Object | Required | Merges changes from detached instance to persistent object |
Additional Details
Transactions and Flush Modes
All Hibernate save methods require managing transaction boundaries appropriately. Although save() and persist() are both used to transition a transient entity into a persistent state, their adherence to JPA specifications and the timing of entity persistence have important distinctions. Understanding the flush mode is also critical, as Hibernate may defer database insertions or updates until a flush operation.
Identifier Generation
The choice between save() and persist() may depend on whether you need the identifier immediately, which save() provides. Conversely, persist() adheres more strictly to JPA semantics by deferring these actions and is identity generator agnostic.
Conclusion
Selecting the right saving method in Hibernate is crucial for maintaining robust and efficient data persistence layers. save(), persist(), saveOrUpdate(), and merge() each have specific use cases and constraints. Understanding how each method interacts within a transaction and how it manages entity states will enable developers to leverage Hibernate effectively and improve the performance of their Java applications.
Related reading
- What are the differences between utf8_general_ci and utf8_unicode_ci?
- What are the implications of using lightweight transactions?
- What are the options for storing hierarchical data in a relational database?
- What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
- What are the effects of exceptions on performance in Java?
- What are the main uses of yield, and how does it differ from join and interrupt?
- What are the pros and cons of DynamoDB with respect to other NoSQL databases?
- What are the pros and cons of DynamoDB with respect to other NoSQL databases?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.