The main intention of the merge method is to update a persistent entity instance with new field values from a detached entity instance.įor instance, suppose we have a RESTful interface with a method for retrieving a JSON-serialized object by its id to the caller, and a method that receives an updated version of this object from the caller. The save call on a detached instance creates a new persistent instance and assigns it a new identifier, which results in a duplicate record in the database upon committing or flushing. The difference comes when we try to save a detached instance: Person person = new Person() The effect of saving an already persisted instance is the same as with persist. The documentation for this method strictly states that it persists the instance, “first assigning a generated identifier.” The method will return the Serializable value of this identifier: Person person = new Person() Its purpose is basically the same as persist, but it has different implementation details. The save method is an “original” Hibernate method that doesn't conform to the JPA specification. The second call to session.persist() causes an exception, so the following code won't work: In the following example, we'll persist the entity, evict it from the context so it becomes detached, and then try to persist again. But if we try to persist a detached instance, the implementation will throw an exception. We may call this method on an already persistent instance, and nothing happens. The id won't necessarily be non-null after we call this method, so we shouldn't rely upon it. The specification for the persist method allows the implementation to issue statements to generate the id on commit or flush. The spec doesn't state that the id will generate right away, regardless of the id generation strategy. Notice that there's nothing here that concerns the identifier of an instance. if an instance is detached, we'll get an exception, either upon calling this method, or upon committing or flushing the session.if an instance is already persistent, then this call has no effect for this particular instance (but it still cascades to its relations with cascade=PERSIST or cascade=ALL).We strictly define the semantics of this method in the specification, which basically states that a transient instance becomes persistent (and the operation cascades to all of its relations with cascade=PERSIST or cascade=ALL): The main differentiating feature of this method is that it conforms to the JSR-220 specification (EJB persistence). This method is a later addition to the Session interface. The person variable references the actual persisted object. It operates on the passed object “in place,” changing its state. Notice that the persist method has a void return type. The generation of INSERT statements will occur only upon committing the transaction, or flushing or closing the session. The object is in the persistence context now, but not yet saved to the database. What happens after we call the persist method? The person object has transitioned from a transient to persistent state. We usually call it when we want to add a record to the database (persist an entity instance): Person person = new Person() transitioning an instance from a transient to persistent state. The persist method is intended to add a new entity instance to the persistence context, i.e. The persistent instance is “online,” whereas the detached instance is “offline” and not monitored for changes. When the entity instance is in the persistent state, all the changes that we make to the mapped fields of this instance will be applied to the corresponding database records and fields upon flushing the Session. Here's a simplified state diagram with comments on Session methods that make the state transitions happen: An instance enters this state if we evict it from the context, clear or close the Session, or put the instance through serialization/deserialization process.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |