Hibernate's Architecture


Hibernate is based on similar ideas and patterns like other ORM solutions (JDO, CMP).

Basic classes used while working with Hibernate are:
- SessionFactory,
- Session,
- Transaction

SessionFactory
An object of this class provides with Session objects. It contains information concerning mapped objects a configuration is being loaded during its formation.An application has usually one object of this class it can be managed by e.g. Spring.

Session one of the most important objects of Hibernate and one of the most troublesome at the beginning. An object of this class represents a unit of work with a database, it usually equals one connection (Connection) with a database. It represents a unit of work with Hibernate. It equals e.g. PersistenceManager in CMP. All persistent objects are attributed to some definite session and any attempt to modify them or even ( sometimes) acces to them results in an exception.

Session is opened by SessionFactory.newSession() , and closed by session.close()
One session should match a logic part of the program an execution of a task. Sometimes ( but not always!) it can be a transaction. While dealing with www applications session-per-request model is very common and easy to implement. Then the session is opened at the beginning of the processing of a claim and closed at the end. The easiest way to do it is by means of the filter. This model can be a bit troublesome if we store some persistent objects in a html sesion ( that is when we use e.g. JSF, Struts)

Transaction - equals one database transaction. It is connected with a concrete session and is made by means of Transaction tx = session.beginTransaction( );

We can cancel it or confirm tx.commit(); tx.rollback();