Hibernate's Main Feature: Servlets or EJB Session Beans
Hibernate's Main Feature: Servlets or EJB Session Beans
The most important Hibernate's feature is mapping from Java classes to database tables (and from Java data types to SQL data types), but also to provides data query ( and retrieval facilities ). It is important that Hibernate generates the SQL calls and relieves keeping the application portable to all SQL databases, with database portability delivered at very little performance overhead.This feature can significialy reduce development time that programmer would have to spent with manual data handling in SQL and JDBC.
Reasuming Hibernate allows to create high-performance database applications with Java much faster and easier. Thanks to transparent persistence for Plain Old Java Objects (POJOs) all you need to do is build a simple POJO, next create XML mapping file that will describe relationship between the databse and the class attributes and at the end call Hibernate API's to specify the operations You can use Hibernate as in standalone Java applications or as in Java EE applications using servlets or EJB session beans.
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
What is a persistence?
Meaning of the persistence is one of the most imporant thing in application development. Nowdays almost all applications require persistent data so it is very important for an system to preserve data entered by users even if the machine was powered off. Persistance in Java ( Hibernate ) is storing data in a relational databse using SQL.
Relational technology provides a way of sharing data among different aplications or among technologies that form part of the same application. Relational technology is a common denominator of many different technology platforms and systems so it is't specific to a particular application. Persistent classes Not only does Hibernate enable a programmer to gain an independence from a given database but it can also manage a state of persistent objects. A programmer may not be aware when and which queries are performed to a database. Objects of classes mapped by Hibernate may appear in three states: Transient - we create objects in this state by means of new. This kind of object is not connected with any session of Hibernate. It is a normal Java object and can be changed into Persistent state.
Persistent - these kinds of objects are connected with both: data in a database and a given object of Session class. Hibernate detects changes in them and put them to a database at the end of a session. Detached - an object is in this state if it used to be Persistent but an appropriate session was closed.
Later, it may be connected with other session. Creating, saving and loading objects. Almost each Java class may be mapped by Hibernate. It cannot be finished - it is because of the architecture which overloads persistent classes. It should have an identifier - Hibernate uses it to test equality. It must have a default constructor. Mapped fields must be public or have appropriate get/set methods. It is a common practice to use classes with JavaBean pattern (i.e. those having appropriate get/set methods for given fields) as persistent classes.