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 hibernate mapping file
  • Register the mapping file in hibernate configuration
  • Create a simple service 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 properties. Their types in order are Long, String, java.util.Date and java.util.Date.

Hibernate is an Object-Relational-Mapping (ORM) technology, which basically means how a Java object is mapped to a relational database model/table. 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 hibernate configuration file (hibernate.cfg.xml).

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

Create Label.hbm.xml mapping file.

In a maven project this file must be placed under the resources directory. In this example we place it in the org/kodejava/hibernate/mapping directory.

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

Create class.

package org.kodejava.hibernate.model;

import java.util.Date;

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

    // Getters & Setters 

    public String toString() {
        return "Label{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", created=" + created +
            ", modified=" + modified +

Create the class.

This class will handle the CRUD operation for our Label entity. In this example we start with the create/insert operation.

package org.kodejava.hibernate.service;

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

public class LabelService {
    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()


Create the class.

package org.kodejava.hibernate;

import org.kodejava.hibernate.model.Label;
import org.kodejava.hibernate.service.LabelService;

import java.util.Date;

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

        // Creates a Label object we are going to be 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.

Register mapping file in hibernate.cfg.xml.

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        <!-- Mapping to hibernate mapping files -->
        <mapping resource="org/kodejava/hibernate/mapping/Label.hbm.xml"/>

We have the code and the mapping file done. To register the mapping file in 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 Hibernate’s SessionFactory.

Maven Dependencies


Maven Central Maven Central