Following are the most important interfaces of Hibernate which act as base for using Hibernate:
1. org.hibernate.SessionFactory
SessionFactoryincludes all of the metadata about Object/Relational Mapping.SessionFactoryis a thread-safe and immutable.- The JPA equivalent of
SessionFactoryisEntityManagerFactory. - A
SessionFactoryis very expensive to create. So an application should have only oneSessionFactoryfor a given database. - The
SessionFactorymaintains services that are used across all Session(s) such as connection pools, transaction system integrations, second level caches etc. - The
SessionFactoryis immutable and its state can not be changed once created.
2. org.hibernate.Session
Sessionrepresents a conversation between the application and the datastore.Sessionis single-threaded and its implementors are not required to be threadsafe.- JPA equivalent of
SessionisEntityManager. - The Hibernate
Sessionwraps a JDBCjava.sql.Connectionbehind the scenes. - Hibernate
Sessionacts a factory fororg.hibernate.Transactioninstances. - The lifecycle of a
Sessionis bounded by the beginning and end of a logical transaction. - Hibernate
Sessionprovides create, read and delete operations for the entities mapped to the database tables. - A
Sessioninstance 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
Sessionmaintains first level cache of the application domain model.
3. org.hibernate.Transaction
Transactionis an abstraction of underlying transaction management.Transactionis associated withSessionand is initiated by callingbeginTransaction(). By callingcommit()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
TransactionisEntityTransaction. - 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.Queryis deprecated. Useorg.hibernate.query.Queryinstead.
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.
