Hibernate Mapping with Annotations

In the previous post we used the .hbm.xml to map our POJO with the DB table. There is another way we can do that, that does not involve any .xml file. Everything will be done in our POJO, so no extra files needed. That kind of implementation requires at least Java 5, hibernate 3.2.0 core and above and it is based on Annotations. We will use annotations to map our POJO.

The Persons.java class we used in the previous example (and was automatically generated by NetBeans) looked like this:

package gr.persons.entities;

import java.math.BigDecimal;
import java.util.Date;

public class Person  implements java.io.Serializable {

     private BigDecimal id;
     private String name;
     private String surname;
     private Date birthdate;
     private String sex;

    public Person() {
    }

    public Person(BigDecimal id) {
        this.id = id;
    }
    public Person(BigDecimal id, String name, String surname, Date birthdate, String sex) {
       this.id = id;
       this.name = name;
       this.surname = surname;
       this.birthdate = birthdate;
       this.sex = sex;
    }

    public BigDecimal getId() {
        return this.id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Date getBirthdate() {
        return this.birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

And we mapped id through the Persons.hbm.xml.

For the annotated solution we will have to alter a little bit the Persons.java:

package gr.persons.entities;

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "PERSON", schema = "YOUR_SCHEMA")
public class Person implements java.io.Serializable {

    @Id
    @Column(name = "ID", unique = true, nullable = false)
    private BigDecimal id;

    @Column(name = "NAME", length = 50)
    private String name;

    @Column(name = "SURNAME", length = 50)
    private String surname;

    @Temporal(TemporalType.DATE)
    @Column(name = "BIRTHDATE", length = 7)
    private Date birthdate;

    @Column(name = "SEX", length =10)
    private String sex;

    public Person() {
    }

    public Person(BigDecimal id) {
        this.id = id;
    }

    public Person(BigDecimal id, String name, String surname, Date birthdate, String sex) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.birthdate = birthdate;
        this.sex = sex;
    }

    public BigDecimal getId() {
        return this.id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Date getBirthdate() {
        return this.birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

The implementation is pretty straight forward. With the annotation @Entity we defined that the Persons.java is an entity that represents the persistent object. With @Table we defined that the our Persons.java class is mapped with the table PERSONS in the schema YOUR_SCHEMA. We also defined the Primary Key that is the id using the @Id annotation. Finally we used the @Column annotation to map our DB columns.

Now in order for our changes to work we have to change our hibernate.cfg.xml. Instead of mapping the resource of Person.hbm.xml we will now map the class Persons.java:

Before:

<mapping resource="gr/persons/entities/Person.hbm.xml"/>

After:

<mapping class="gr.persons.entities.Person"/>

An other thing we must note is that in our HibernateUtil.java class we create our Session Factory based on AnnotationConfiguration.

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

This is needed for annotations to work. If we use Configuration, it will not work.

P.S Special thanks to SaperDuper that motivated me to write the annotated version of the previous tutorial :)