Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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.

Ads

Tutorials   
When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials
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. 

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.

Advertisements

Ads
Share on Google+Share on Google+

Insert Data into Database Using Hibernate Native SQL

Posted on: September 29, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Insert Data into Database Using Hibernate Native SQL   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
whoisdis
June 23, 2011
can not instantiated configuration

When i am trying to write this.. sessionFact = new Configuration().configure().buildSessionFactory(); It shows a compile time error. - Cannot instantiate the type Configuration - The method configure() is undefined for the type Configuration
szyder
February 6, 2012
Wheres native

Where do you see any handwritten native sql in this example? Thumbs down.
Luis
April 5, 2012
question

And if you want to insert a value (studentName, email, etc) in a especific Row or ID of the table of Students? For example, the typical "where" Insert into STUDENTS (name,email,addres) WHERE ID = '1234'
Luis
April 5, 2012
Question Correction

Sorry i did a terrible estupid thing. I refer to update... not insert. Thanks

Ads

 

Ads