Hibernate: the beginning
Before you start learning some new technology I recommend you find some good article, which make a concise overview of the technology and give several code examples. Hibernate has become the most popular ORM solution in Java development therefore it’s not strange that the most vacancies require its knowledge. This article is challenging for me, because I’m going to make a basic overview of the technology and to provide a code sample with MySQL.
In the end of the tutorial you can find link to example of Hibernate usage.
Theory
The main purpose of Hibernate is to simplify a code related to interaction of an application with a database, because using pure JDBC leads to numerous lines of code. It’s possible due mapping of application’s entities to database tables. Also Hibernate frees you from the manual connection management and annoying exception handling. Part of this work takes SessionFactory.
Let’s look at the abstract application’s scheme below which uses Hibernate as ORM.
Every modern application has some domain models. They represent some objects such as cars, books, contacts, customers etc. For each domain model exists table in database. Every column from table has an appropriate field in the domain model.
As you understand POJOs are used for the persistence of data which are obtained in runtime of the application. POJO contains fields and a set of getters / setters for the fields. This implies that you can perform all range of CRUD operations.
Let’s go back to the scheme. In the center you can see hibernate.cfg.xml, its role is to contain all Hibernate properties (e.g. database url, database user, password, sql dialect etc.). Also if you use xml mapping for the POJO, you need to put separate links on xml-mapping files. XML-mapping is illustrated on the scheme with the dashed arrows.
Example of hibernate.cfg.xml:
<hibernate-configuration> <session-factory> <!-- SET OF PROPERTIES --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibnatedb</property> <property name="hibernate.connection.username">hibuser</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="connection.pool_size">1</property> <property name="show_sql">true</property> <!-- Mapping files --> <mapping resource="person.hbm.xml"></mapping> <mapping resource="contact.hbm.xml"></mapping> <mapping resource="company.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Every “mapping” tag contains link to another XML-file with rules of mapping domain object fileds to table columns. If you use the annotation-based configuration of the POJOs, your hibernate.cfg.xml file will contain “mapping” tags with “class” attribute instead of “resource”. Hence you don’t need to create separate xml-files for every domain model, because you will annotate required fields directly in classes.
Both of the approaches have its own cons and pros, but that’s out of the article’s scope.
Further you can instantiate SessionFactory based on hibernate.cfg.xml. Usually it is one per application. Then you can create a Session object and perform CRUD operations… Hibernate interacts with DB via JDBC API.
Summary
Everything you have read above is a rough overview of Hibernate and its functional principles of functionality. I called it rough, because it doesn’t contain a lot of details (e.g. Transactions). For more information you can read books and documentation. The last one is perfect.
In the second part, I will show you how to develop simple application using Hibernate.