Hibernate is a well known object-relational mapping library (ORM) for Java. It allows you to map entries in your relational database to objects in your Java classes.
Usually this is done by a mapping file written in XML. As this approach is a bit more expensive we will use annotations to map a class to the relating database table.
Based on the Maven tutorial by my colleague Phillip, we will begin with a quickstart archetype and include Hibernate and a MySQL connection. Then we will write a simple data access object (DAO) which maps to the database table we will create. After that we use a class to set up the connection and fill the database with some of our objects.
Note: This is just a superficial glance at Hibernate to get in touch with the framework.
Setting up the project
After generating the quickstart archetype we will extend the pom.xml. Add the following dependencies:
<!-- hibernate with annotations for persistence --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.3.1.GA</version> </dependency> <!-- connector to mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
The first two extensions add hibernate support and annotation support. The third one provides the the connection to MySQL.
The Database Parts
Now we will create a simple database table to store our data and add a corresponding class. Let’s pretend we want to store interesting coordinates. We just use a description, the longitude and latitude. Additionally we use an id as our primary key which will autoincrement.
CREATE TABLE `hibernateexample`.`coordinatestorage` ( `id` INT NOT NULL AUTO_INCREMENT, `description` VARCHAR(100) NOT NULL, `longitude` FLOAT NOT NULL, `latitude` FLOAT NOT NULL, PRIMARY KEY (`id`) )
The Java class looks according, using annotations for the mapping.
package com.unitedcoders.dao; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "coordinatestorage") public class CoordinateStorage { @Id @GeneratedValue Integer id; @Column String description; @Column Float longitude; @Column Float latitude; // ... following Getter and Setter, cut out for brevity
That’s about all we need to store the data but we’re still lacking the connection to the database. Create a hibernate.properties file in you classpath and fill it with the appropriate values.
hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost/hibernateexample hibernate.connection.username=root hibernate.connection.password=root
The Application
To test the persistence classes we will set up a simple Java class, get a session, create some objects and save them into the database.
package com.unitedcoders; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.classic.Session; import com.unitedcoders.dao.CoordinateStorage; public class FillDatabase { public static void main(String[] args) { AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration.addAnnotatedClass(CoordinateStorage.class); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); for (Float i = 0.0F; i < 10.0F; i++) { CoordinateStorage coStorage = new CoordinateStorage(); coStorage.setDescription("Location " + i); coStorage.setLatitude((i * i * 12) % 360); coStorage.setLongitude((i * i * 55) % 360); session.save(coStorage); } session.close(); sessionFactory.close(); } }
Final Words
As you can see it's fairly simple to get Hibernate integration in your project. We just looked a very minimal feature set here and integrated Hibernate manually. For a real application you would configure the project differently.
For more information refer to the Hibernate documentation and especially to the available annotations.

Nico Heid

Latest posts by Nico Heid (see all)
- Raspberry Pi Supply Switch, start and shut down your Pi like your PC - August 24, 2015
- Infinite House of Pancakes – code jam 2015 - July 13, 2015
- google code jam 2014 – magic trick - April 18, 2014