Quick Contact

    Hibernate

    Hibernate Session Interface Methods

    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.

    Overview of Session Interface

    The Session interface is the main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.

    The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

    The main methods of the Session interface are to create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:

    • transient −

      A new instance of a persistent class, which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.

    • persistent −

      You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.

    • detached −

      Once we close the Hibernate Session, the persistent instance will become a detached instance.

    Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by a get() or load() method is persistent. Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().

    There are a number of methods provided by the Session interface, but I’m going to list down a few important methods that have been frequently used:

    1. Transaction beginTransaction() –

      Begin a unit of work and return the associated Transaction object.

    2. voidcancelQuery() –

      Cancel the execution of the current query.

    3. void clear() –

      Completely clear the session.

    4. Connection close() –

      End the session by releasing the JDBC connection and cleaning up.

    5. Criteria createCriteria(Class persistentClass) –

      Create a new Criteria instance, for the given entity class, or a superclass of an entity class.

    6. Criteria createCriteria(String entityName) –

      Create a new Criteria instance, for the given entity name.

    7. SerializablegetIdentifier(Object object) –

      Return the identifier value of the given entity as associated with this session.

    8. Query createFilter(Object collection, String queryString) –

      Create a new instance of Query for the given collection and filter string.

    9. Query createQuery(String queryString) –

      Create a new instance of Query for the given HQL query string.

    10. SQLQuerycreateSQLQuery(String queryString) –

      Create a new instance of SQLQuery for the given SQL query string.

    11. void delete(Object object) –

      Remove a persistent instance from the datastore.

    12. void delete(String entityName, Object object) –

      Remove a persistent instance from the datastore.

    13. Session get(String entityName, Serializable id) –

      Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

    14. SessionFactorygetSessionFactory() –

      Get the session factory which created this session.

    15. void refresh(Object object) –

      Re-read the state of the given instance from the underlying database.

    16. Transaction getTransaction() –

      Get the Transaction instance associated with this session.

    17. booleanisConnected() –

      Check if the session is currently connected.

    18. booleanisDirty() –

      Does this session contain any changes which must be synchronized with the database?

    19. booleanisOpen() –

      Check if the session is still open.

    20. Serializablesave(Object object) –

      Persist the given transient instance, first assigning a generated identifier.

    21. voidsaveOrUpdate(Object object) –

      Either save(Object) or update(Object) the given instance.

    22. void update(Object object) –

      Update the persistent instance with the identifier of the given detached instance.

    23. void update(String entityName, Object object) –

      Update the persistent instance with the identifier of the given detached instance.

    Key Points

    • A Session instance is serializable if its persistent classes are serializable.
    • If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.