Hibernate tutorial with Eclipse

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

Hibernate is an object/relational mapping tool for Java environments. What does it mean the term object/relational mapping? simply a technique of mapping a data representation from an object model to a relational data model with a SQL-based schema.

Hibernate tutorial with Eclipse

For this tutorial we'll use the latest stable release of Hibernate 5 which you can download here: http://www.hibernate.org/downloads 

The tutorial will be built using Eclipse, anyway, since we'll create the Project as simple java project you can easily adapt it on any other IDE. Ok, at fist let's create a new Java project. We'll name it "Hibernate tutorial".

Hibernate 5 tutorial Eclipse jboss

Next step will be configuring the Libraries needed to run the Project. We will be adding:

1. A New User Library containing all the JAR files included in the hibernate-release-5.X.X.Final/lib/required

Creating an User Library

An User Library can be created by selecting the Libraries tab | Add Library | User Library. Click on the User Library button and select New. Enter a name for it and add Hibernate libraries by clicking on Add External JARs:

hibernate tutorial Eclipse jboss

2. A JDBC Driver for connecting to the Database. In our case we will use MySQL JDBC Driver:

hibernate libs tutorial eclipse jboss

Adding Java Classes and configuration files

Done with the Project setup, it's time to add Entities which will represent the relational tables. We'll add a Java class named com.sample.model.Person. As you can see, this Class is a sample JavaBean with properties and getters/setters

package com.sample.model;

public class Person {
    Long id;
    String name;
    String surname;
    String address;

    public Long getId() {
        return id;
    }
    private void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}           

Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.

Create a file named Person.hbm.xml in the same package/folder of your JavaBean Person.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.sample.model.Person" table="PERSON">

        <id name="id" column="ID">
            <generator class="native" />
        </id>

        <property name="name">
            <column name="NAME" length="16" not-null="true" />
        </property>

        <property name="surname">
            <column name="SURNAME" length="16" not-null="true" />
        </property>

        <property name="address">
            <column name="ADDRESS" length="16" not-null="true" />
        </property>

    </class>
</hibernate-mapping>
  • The id element is the declaration of the identifier property, name="id" declares the name of the Java property - The column attribute tells Hibernate which column of the PERSON table we use for this primary key.
  • The nested generator element specifies the identifier generation strategy: in this case we used native, which picks the best strategy depending on the configured database (dialect).
  • The fields are enlist as "property". Notice the column attribute is added on every property even if this can be skipped if java property = database field.