Home Hibernate Hibernate : Dynamic-insert



Hibernate : Dynamic-insert
Posted on: August 17, 2012 at 12:00 AM
This tutorial contains description of Hibernate dynamic-insert.

Hibernate : Dynamic-insert

This tutorial contains description of Hibernate dynamic-insert.

Hibernate : Dynamic-insert :

Hibernate allows you to use dynamic-insert. It is optional and by default it's value is false.
When dynamic-insert is true it means INSERT SQL is generated at runtime and contains only those columns whose value are set.
Hibernate generates corresponding SQL each time for dynamic-insert.
It is good to use dynamic-insert or dynamic-update where number of columns in a table and you want to set value in some of them.

You can use dynamic-insert in two ways -by using annotation or in xml file.

Example - Here is an example of dynamic-insert. We are using annotation to set dynamic-insert.

Student.java -

package net.roseindia.table;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
@org.hibernate.annotations.Entity(
dynamicInsert = true)                //
public class Student implements Serializable {

@Id
@GeneratedValue
@Column(name="roll_no")
private int roll;

@Column(name="name")
private String name;

@Column(name="course")
private String course;

public Student() {

}

public int getRoll() {
return roll;
}

public void setRoll(int roll) {
this.roll = roll;
}

public String getName() {
return name;
}

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

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}

}

Main class HibernateDynamicInsert.java-

package net.roseindia.main;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateDynamicInsert {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Student student = new Student();
student.setName("Justin");
session.save(student);
transaction.commit();
System.out.println("Record saved");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}

Output :

Hibernate: insert into student (name) values (?)
Record saved

If you assign dynamicInsert =false then your output is -

Hibernate: insert into student (course, name) values (?, ?)
Record saved

Click here to download complete source code

Related Tags for Hibernate : Dynamic-insert:


More Tutorials from this section

Ask Questions?    Discuss: Hibernate : Dynamic-insert  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.