0% found this document useful (0 votes)
48 views

Hibernate's Main Feature: Servlets or EJB Session Beans

Hibernate is an object-relational mapping tool for Java that allows developers to create high-performance database applications with Java much faster and easier. It provides transparent persistence for Plain Old Java Objects (POJOs) by mapping Java classes to database tables and Java data types to SQL data types. This allows developers to focus on building the Java objects rather than having to write manual SQL and JDBC code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Hibernate's Main Feature: Servlets or EJB Session Beans

Hibernate is an object-relational mapping tool for Java that allows developers to create high-performance database applications with Java much faster and easier. It provides transparent persistence for Plain Old Java Objects (POJOs) by mapping Java classes to database tables and Java data types to SQL data types. This allows developers to focus on building the Java objects rather than having to write manual SQL and JDBC code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Hibernate's Main Feature

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

means of Transaction tx = session.beginTransaction( ); We can cancel it or confirm tx.commit(); tx.rollback();

Object Relation Mapping in Hibernate


ORM - Object Relation Mapping. Almost all contemporary databases are relational and based on SQL language. Most programs using them are created in an object way. So some kind of incoherence appears at the meeting point of a database and software. We need some appropriate tools to translate data from relational language into an object language and the other way round. Service of JDBC is extremely troublesome while dealing with more complex applications. ORM deals with following tasks: Hibernate is currently one of the most popular ORM solutions.Its popularity and effectiveness were proved by the fact that e.g. implementation of CMP in EJB3 on JBoss server is based on Hibernate. What is more, a lot of solutions were copied during designing EJB3 specification. Other, common solutions ORM include: CMP, JDO. Hibernate, as opposed to them, is not a standard but a concrete solution. Its main advantage is: - lightness it is no need to use special containers, it is enough to attach an appropriate library. One can also use it to both: web and client applications. There is no need to generate manually an additional code. As opposed to e.g. JDO, Hibernate requires only presence of configuration files. Additional classes used by it are generated during performing a task.

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.

Subtypes and association problems


The problem of subtypes Let us imagine a given situation: User class is connected with BillingDetails. During performing on class object BillingDetails an instance of one of the subclasses of this class can be ascribed. Therefore the User object will actually refer to an object from different class than BillingDetails. It is a quite common way of conduct as far as an object programming is concerned. We would like to have a similar opportunity while dealing with databases, however, it is not possible that any field in a table (here: a foreign key) may refer to some fields in a few different tables. That is why while saving similar situations in a database we have to choose another solution.
The problem of association The following situation can happen in Java: While mapping this kind of situation to a database we have to bear in mind that we cannot directly save many-to-many paradigm and we have to create an intermediate table. Hibernate (like other similar technologies, e.g. JDO, Entity Beans) meets our expectations. Hibernate becomes an additional layer in our application, a bridge between the logic of an application and a database (object/relational mapping). It enables us to define objects storing data ( as typical Java class), which can be used in our application and which are mapped to a database in a definite way. Hibernate provides us also with its own query language, structurally very similar to SQL. Both these facts make an application using Hibernate more portable.

You might also like