Learnitweb

Important interfaces of Hibernate

Following are the most important interfaces of Hibernate which act as base for using Hibernate:

1. org.hibernate.SessionFactory

  • SessionFactory includes all of the metadata about Object/Relational Mapping.
  • SessionFactory is a thread-safe and immutable.
  • The JPA equivalent of SessionFactory is EntityManagerFactory.
  • A SessionFactory is very expensive to create. So an application should have only one SessionFactory for a given database.
  • The SessionFactory maintains services that are used across all Session(s) such as connection pools, transaction system integrations, second level caches etc.
  • The SessionFactory is immutable and its state can not be changed once created.

2. org.hibernate.Session

  • Session represents a conversation between the application and the datastore.
  • Session is single-threaded and its implementors are not required to be threadsafe.
  • JPA equivalent of Session is EntityManager.
  • The Hibernate Session wraps a JDBC java.sql.Connection behind the scenes.
  • Hibernate Session acts a factory for org.hibernate.Transaction instances.
  • The lifecycle of a Session is bounded by the beginning and end of a logical transaction.
  • Hibernate Session provides create, read and delete operations for the entities mapped to the database tables.
  • A Session instance is serializable if its persistent classes are serializable.
  • If an exception is thrown in a Session, the transaction must be rolled back and the session is discarded.
  • Hibernate Session maintains first level cache of the application domain model.

3. org.hibernate.Transaction

  • Transaction is an abstraction of underlying transaction management.
  • Transaction is associated with Session and is initiated by calling beginTransaction(). By calling commit() method, commits the current resource transaction, writing any unflushed changes to the database.
  • Transaction is single-threaded and is used to define the physical transaction boundaries.
  • The JPA equivalent of Transaction is EntityTransaction.
  • Multiple transactions can be associated with a session or we can say that a session can span multiple transactions. But at a given time there should be only one uncommitted transaction associated with a session.

Other important interfaces

4. org.hibernate.query.Query

  • Interface used to control query execution.
  • Represents an HQL/JPQL query or a compiled Criteria query.
  • Hibernate extension to the JPA Query/TypedQuery contract.

org.hibernate.Query is deprecated. Use org.hibernate.query.Query instead.

5. org.hibernate.Cache

  • Provides an API for querying and managing the second level cache.
  • Its methods do not adhere any isolation or transactional semantics associated with the underlying caches.

6. org.hibernate.Criteria

  • Provides an API to retrieve entities.
  • Criteria is simple to use when the number of conditions to be applied on the resultset are variable.