• SQL Tutorial
  • Servlet
  • Spring
  • JSP
  • Java IO
  • JavaScript
  • Struts
  • jQuery
  • RubyonRails

  • <

    Hibernate Session

    A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
    The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed. The main function of the Session is to offer, create, read, and delete operations for instances of mapped entity classes.

    The persist method is intended for adding a new entity instance to the persistence context, i.e. transitioning an instance from transient to persistent state.

    The save method is an original Hibernate method that does not conform to the JPA specification.
    Its purpose is basically the same as persist, but it has different implementation details. The documentation for this method strictly states that it persists the instance, first assigning a generated identifier. The method is guaranteed to return the Serializable value of this identifier.

    The main intention of the merge method is to update a persistent entity instance with new field values from a detached entity instance.
    For instance, suppose you have a RESTful interface with a method for retrieving an JSON-serialized object by its id to the caller and a method that receives an updated version of this object from the caller. An entity that passed through such serialization/deserialization will appear in a detached state.
    After deserializing this entity instance, you need to get a persistent entity instance from a persistence context and update its fields with new values from this detached instance.

    As with persist and save, the update method is an ‚??original‚?? Hibernate method that was present long before the merge method was added. Its semantics differs in several key points:
  • it acts upon passed object (its return type is void); the update method transitions the passed object from detached to persistent state;
  • this method throws an exception if you pass it a transient entity.

  • SaveOrUpdate
    This method appears only in the Hibernate API and does not have its standardized counterpart. Similar to update, it also may be used for reattaching instances.
    Actually, the internal DefaultUpdateEventListener class that processes the update method is a subclass of DefaultSaveOrUpdateListener, just overriding some functionality. The main difference of saveOrUpdate method is that it does not throw exception when applied to a transient instance; instead, it makes this transient instance persistent.

    © copyright 2017-2021 Completedone pvt ltd.