How do I store object in Hibernate?

This example shows you how to store or save Hibernate object to database. The basic steps in creating application in Hibernate will be:

  • Creates the Pojo
  • Create the Hibernate mapping file
  • Register the mapping file in the Hibernate configuration
  • Create a simple manager class to store the object

In this example we’ll create a class called Label, this class is about a record label company. This class has the id, name, created and modified attributes. Their types in order are Integer, String, java.util.Date and java.util.Date.

Hibernate is an Object Relation Mapping technology which basically means how an object is mapped to a relational database model. Because of this it needs a mapping file to map the object properties to table columns. The mapping file usually named in the format of Label.hbm.xml, the class name with hbm.xml suffix. And for Hibernate application recognize the object, the mapping file should be registered in the Hibernate configuration hibernate.cfg.xml file.

We have a brief introduction about the Hibernate class and configuration structure. Let’s jump the the working example. First we creates the mapping file and then we create the classes.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="org.kodejava.example.hibernate.model.Label" table="LABELS">
        <id name="id" column="id">
            <generator class="native"/>
        <property name="name" not-null="true"/>
        <property name="created" column="created" type="timestamp"/>
        <property name="modified" column="modified" type="timestamp"/>
package org.kodejava.example.hibernate.model;

import java.util.Date;

public class Label {
    private Integer id;
    private String name;
    private Date created;
    private Date modified;

    // Getters and Setters

    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        sb.append("id = ").append(id).append("; ");
        sb.append("name = ").append(name).append("]");
        return sb.toString();

import org.hibernate.Session;
import org.kodejava.example.hibernate.model.Label;

public class LabelManager {
    public void saveLabel(Label label) {
        // To save an object we first get a session by calling 
        // getCurrentSession() method from the SessionFactoryHelper class. 
        // Next we create a new transaction, save the Label object and 
        // commit it to database,
        Session session = SessionFactoryHelper.getSessionFactory()


import org.kodejava.example.hibernate.model.Label;

import java.util.Date;

public class InsertDemo {
    public static void main(String[] args) {
        LabelManager manager = new LabelManager();

        // Creates a Label object we are going to stored in the database.
        // We set the name, modified by and modified date information.
        Label label = new Label();
        label.setName("Sony Music");
        label.setCreated(new Date());

        // Call the LabelManager saveLabel method.

We have the code and the mapping file done. To register the mapping file in the Hibernate configuration file you can see the How do I create Hibernate’s SessionFactory? example. The example also tells you how to create the SessionFactoryHelper class to obtain the Hibernate’s SessionFactory.


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA. Support me by donating >> here <<.

Leave a Reply