Insert Data into Database Using Hibernate Native SQL

Ads
 

Insert Data into Database Using Hibernate Native SQL

In this example we will show you how you can use Native SQL with hibernate. You will learn how to use Native to insert data into database. Native SQL is handwritten SQL for all database operations like insert, update, delete and select.

In this example we will show you how you can use Native SQL with hibernate. You will learn how to use Native to insert data into database. Native SQL is handwritten SQL for all database operations like insert, update, delete and select.

Insert Data into Database Using Hibernate Native SQL

     

In this example we will show you how you can use Native SQL with hibernate. You will learn how to use Native to insert data into database. Native SQL is handwritten SQL for all database operations like insert, update, delete and select. 

Hibernate provides a powerful query language Hibernate Query Language that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application.


Step1: Create hibernate native sql for inserting data into database.

Hibernate Native uses only the Hibernate Core for all its functions. The code for a class that will be saved to the database is displayed below:

package hibernateexample;

import javax.transaction.*;
import org.hibernate.Transaction;
import org.hibernate.*;
import org.hibernate.criterion.*;
import org.hibernate.cfg.*;
import java.util.*;


public class HibernateNativeInsert {
  public static void main(String args[]){
  Session sess = null;
  try{
  sess = HibernateUtil.currentSession();
  Transaction tx = sess.beginTransaction();
  Studentdetail student = new Studentdetail();
  student.setStudentName("Amardeep Patel");
  student.setStudentAddress("rohini,sec-2, delhi-85");
  student.setEmail("[email protected]");
  sess.save(student);
  System.out.println("Successfully data insert in database");
  tx.commit();
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  finally{
  sess.close();
  }
  }
}

 

Step 2: Create session factory 'HibernateUtil.java'.

Here is the code of session Factory:

package hibernateexample;

import java.sql.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.*;

public class HibernateUtil {
  public static final SessionFactory sessionFact;
  static {
  try {
  // Create the SessionFactory from hibernate.cfg.xml
  sessionFact = new Configuration().configure().buildSessionFactory();
  }
  catch(Throwable e) {
  System.out.println("SessionFactory creation failed." + e);
  throw new ExceptionInInitializerError(e);
  }
  }
  public static final ThreadLocal session = new ThreadLocal();
  
  public static Session currentSession() throws HibernateException {
  Session sess = (Session) session.get();
  // Open a new Session, if this thread has none yet
  if(sess == null){
  sess = sessionFact.openSession();
  // Store it in the ThreadLocal variable
  session.set(sess);
  }
  return sess;
  }
  public static void SessionClose() throws Exception {
  Session s = (Session) session.get();
  if (s != null)
  s.close();
  session.set(null); 
  }
}

 

Step 3: Hibernate native uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column. Here is the code for "Studenetdetail.java":

 

package hibernateexample;

public class Studentdetail {
  private String studentName;
  private String studentAddress;
  private String email;
  private int id;
  
  public String getStudentName(){
  return studentName;
  }
  
  public void setStudentName(String studentName){
  this.studentName = studentName;
  }
  
  public String getStudentAddress(){
  return studentAddress;
  
  }
  
  public void setStudentAddress(String studentAddress){
  this.studentAddress = studentAddress;
  }
  
  public String getEmail(){
  return email;
  }
  
  public void setEmail(String email){
  this.email = email;
  }
  
  public int getId(){
  return id;
  }
  
  public void setId(int id){
  this.id = id;
  }
}

 

Here is the output:

Download here all application.

Ads

Ads