Hibernate operations (Data insertion) - Part 1

Hibernate is an object-relational mapping (ORM) library for the Java language. Hibernate provides data query and retrieval facilities that significantly reduce the development time. Hibernate facilitates you to develop persistent classes following an object-oriented styles such as inheritance, polymorphism, composition, association, and collections.

This article only concern about data insertion to the db through hibernate.

And also I have to mention here this article not describing all the hibernate from the beginning. Therefore this is not the best way to start your first hibernate java project. Simply this article very much suitable for the people those who having basis of the hibernate.

Hibernate is a ORM tool. Therefore it is not necessary to care about db type (whether MySql or derbydb or another) when you programming a application for a db. But you have to concern only about the 'connection.driver_class' and 'connection.url' in the 'hibernate.cfg.xml' file to match the db type.

Therefore you can change your database type at anytime without changing whole java codes or queries written for da operations. It is the power of Hibernate.

To start these all the sample programs you should do the initial thing such as adding related jar files, referring those jars, establish MySql db connection(from eclipse or netbeans to view db's data), etc.

First let's create java class which used as a persistent class.
Create a java class named "Person.java"

package pkg;
import javax.persistence.Entity;
import javax.persistence.Id;

public class Person {
 private int id;
 private String name;
 private int age;

 public int getId() {
  return id;
 public void setId(int id) {
  this.id = id;
 public String getName() {
  return name;
 public void setName(String name) {
  this.name = name;
 public int getAge() {
  return age;
 public void setAge(int age) {
  this.age = age;

This class is now persistent class through annotations.

Data insertion
Data insertion can be done using this persistent class's object. Following sample code shows the way to insert data to a database through hibernate.

 Create a java class named "TestPerson.java" with main method.

package pkg;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestPerson {

 public static void main(String[] args) {

  //create configurations for Person class
  AnnotationConfiguration config = new AnnotationConfiguration();

  //create new schema(db and table)
  new SchemaExport(config).create(true, true);//comment this line if you already created the table Person
  SessionFactory factory = config.buildSessionFactory();
  Session session = factory.getCurrentSession();
  session.beginTransaction();//begin the db operations
  //Insertion to the table
  Person person = new Person();//create the object from persistent class
  //set data that you want to store in db
  session.save(person);//save the object to db
  session.getTransaction().commit();//save permanently to db(end of db operations)

Now data(id=1, name=Milinda and age=24) will be stored in the 'Person' table. Finally the 'Person' table's data can be shows as follows.

| id | age | name     |
|  3 |  24 | Milinda  |

Note- You can remove(or comment) the code line new SchemaExport(config).create(true, true); if you already create the db and particular tables.(This code line responsible for create tables on th db)

I hope to post more articles for other operations of hibernate such as
  • Data update
  • Data retrieve
  • Data delete
from the db. Will be soon........

Related posts

No comments:

Post a Comment