My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Hibernate Core Interfaces

The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.
  • Configuration interface
  • Transaction interface
  • SessionFactory interface
  • Session interface
  • Query and Criteria interfaces
1. Configuration:
The Configuration object is used to configure hibernate. The application uses a Configuration instance to specify the location of mapping documents and Hibernate-specific properties and then create the SessionFactory.
Configuration cfg = new Configuration();
cfg.addResource("hello/Message.hbm.xml");
cfg.setProperties( System.getProperties() );
2. Transaction:
A transaction represents a unit of work. Application uses transactions to do some operations on DB. Within one transaction you can do several operations and can commit transaction once after successfully completed all operations. The advantage here is you can rollback all previous operations if one operation is fail in your operation batch.
The Transaction does not get committed when session gets flushed.
The Transaction interface is an optional API. Hibernate applications may choose not to use this interface, instead managing transactions in their own infrastructure code. A Transaction abstracts application code from the underlying transaction implementation-which might be a JDBC transaction, a JTA UserTransaction, or even a Common Object Request Broker Architecture (CORBA) transaction-allowing the application to control transaction boundaries via a consistent API. This helps to keep Hibernate applications portable between different kinds of execution environments and containers.
Transaction transaction = session.beginTransaction();

3. SessionFactory:
The application obtains Session instances from a SessionFactory. SessionFactory instances are not lightweight and typically one instance is created for the whole application. If the application accesses multiple databases, it needs one per database.
SessionFactory can hold an optional (second-level) cache of data that is reusable between transactions at a process, or cluster, level.
SessionFactory sessionFactory =cfg.buildSessionFactory();

4. Session:
The Session is a persistence manager that manages operation like storing and retrieving objects. Instances of Session are inexpensive to create and destroy. They are not thread safe.
Session holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.

Session session = sessionFactory.openSession();
5. Query and Criteria interfaces:
The Query interface allows you to perform queries against the database and control how the query is executed. Queries are written in HQL or in the native SQL dialect of your database. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.
The Criteria interface is very similar; it allows you to create and execute object-oriented criteria queries.
To help make application code less verbose, Hibernate provides some shortcut methods on the Session interface that let you invoke a query in one line of code. We won't use these shortcuts; instead, we'll always use the Query interface.
A Query instance is lightweight and can't be used outside the Session that created it.