JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Hibernate tutorial with Eclipse

 

 

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 Hibernate 3.5.6 GA which you can download here:

 

http://www.hibernate.org/downloads 

The tutorial will be built using Eclipse Enterprise, 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 tutorial example

Next step is adding a Java class which will be mapped on the DB. We'll create class com.sample.Person :

hibernate tutorial example

Class is a sample JavaBean with properties and getters/setters

package com.sample;

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.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.

 
Advertisement



Francesco Google+
Top Programming Sites
You are here Projects Hibernate Quickstart tutorials Hibernate/JPA Hibernate tutorial with Eclipse