Monday 21 November 2016

Hibernate 4.3 Introduction Tutorial

An introduction to Hibernate 4

In this article I will show you how can you use Hibernate 4,  along with a simple example application available to try out what you read about.




What is Hibernate and why should I care?

Hibernate was created to leverage the connection between Java applications and relational databases because it is hard to map back and forth between a database table and a Java object.
And because Hibernate does this, it reduces development time which is consumed by JDBC query-execution and data mapping.

Getting Hibernate

To get the latest version of Hibernate, just visit this site. For this article I will use the version 4.3.10.Final.
If you download and extract the package you can see some sub-folders in the lib folder. Everything under required is required for any project using Hibernate. The other folders contain libraries for special cases. For example under jpa you find the library providing the JPA entity manager support.
Alternatively you can set up a Maven project and add Hibernate as a dependency. In this case you do not need to take care about the other required dependencies of Hibernate, which come along in the required package with the bundle-download. Using Maven is more simple and straightforward so I will use Maven as dependency management.

A simple example

In the simple example I will create a Java application which stores information about books in the database. The database will be an H2 memory database for the sake of simplicity.
Dependencies are managed with Maven and the output is an executable JAR with all dependencies.

The entity

The entity I will store in the database is the following:
The no-argument constructor is a requirement for all persistent classes because Hibernate creates the object instances per reflection. In this case this constructor is private to prevent creation of books without information.

The dependencies

To get the app running, we need two dependencies in the project: Hibernate and H2. To do this, add the following to the pom.xml:
Now we are ready to continue on.

Configuring Hibernate

Hibernate needs some configuration to get started. This you need to include in the hibernate.cfg.xml file. It is plain old XML. It contains the database connection properties and the entity mapping files inclusive location.

The entity mapping

To map the right fields to the right column in the database, Hibernate requires a mapping file for the entities. These are located in the .hbm.xml files which start with the entity’s name. In this example Book.hbm.xml.

The main method

To use the application with Hibernate we still need an entry point — and in Java this is the main method. To start, we need some configuration, like creating a session with a session factory… So let’s look at the code how it goes:

After running the application the console should have some log messages and the one book added to the database:
—-
—–

Conclusion

Hibernate gives a nice feature to leverage mapping between Java objects and relational databases. Naturally this sample application does not show the full power of Hibernate: for better user experience you could add a user interface to create and list books in the application.
In a later article I will show how to get rid of the XML configuration (called by some developers as the “XML-Hell”) and use annotations instead. So stay tuned.

Source:javabeginnerstutorial.co

No comments:

Post a Comment