Hibernate Insert

Ads
 
Tutorials   
Hibernate Java - Hibernate Open Source ORM tool in Java What is difference between Hibernate 4 and 5? Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated 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

Hibernate Insert

This tutorial will help you to learn how to insert data into table by using hibernate.

This tutorial will help you to learn how to insert data into table by using hibernate.

Hibernate Insert

This tutorial will help you to learn how to insert data into table by using hibernate.

Hibernate insert example : Hibernate provides 3 ways to insert data into database table.

1. By using native SQL

2.By using Hibernate Query language(HQL).

3.By using hibernate criteria.

In this section we are giving an insertion example by using native SQL.

Native SQL:  Hibernate provides native SQL to express database queries. Then pass a string containing the sql query to the method createSQLQuery().

Example -In this example student is our table in which we are inserting roll_no , name and course.

We used here native SQL as - String sql = "INSERT INTO student(roll_no, name,course) VALUES(12, 'Roxi' ,'Hibernate')";

createSQLQuery() method is used to call our native SQL statement directly by using session.

Here is your code -

package net.roseindia.main;

import net.roseindia.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {

transaction = session.beginTransaction();
String sql = "INSERT INTO student(roll_no, name,course) VALUES(12, 'Roxi' ,'Hibernate')";
Query query = session.createSQLQuery(sql);
query.executeUpdate();
session.getTransaction().commit();

session.flush();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}

Output:

Hibernate: insert into student(roll_no, name,course) values(12, 'Roxi' ,'Hibernate')
New record inserted

Click here to download complete source code

Ads

Ads