insert data into table using hibernate example

insert data into table using hibernate example

Let’s take one simple insert data into table using hibernate example , and understand how to insert some record in database using Hibernate framework.

Insert data into table using hibernate example

Here i will use oracle 10G database and java 8 version , Please make sure before running below program you have to add all hibernate jar files into lib folder and configure build path because this example is on standalone application.

  1. hibernate.cfg.xml
  2. studentinfo.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 <property name="">update</property>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
 <property name="connection.username">system</property>
 <property name="connection.password">manager</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <mapping resource="com/javagf/bean/StudentInfo.hbm.xml" />


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated 16 Jun, 2017 7:57:05 PM by Hibernate Tools 3.5.0.Final -->
 <class name="com.javagf.bean.StudentInfo" table="STUDENTINFO">
 <id name="roll" type="int">
 <generator class="assigned" />
 <property name="name" type="java.lang.String"></property>
 <property name="address" type="java.lang.String"></property>

package com.javagf.bean;
public class StudentInfo {
 private int roll;
 private String name; 
 private String address;
 public int getRoll() { return roll; }
 public void setRoll(int roll) { this.roll = roll; }
 public String getName() { return name; }
 public void setName(String name) { = name; }
 public String getAddress() { return address; }
 public void setAddress(String address) { this.address = address; }

package com.javagf.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.StudentInfo;
public class Run {
 public static void main(String[] args) {
 Configuration cfg = new Configuration(); 
 SessionFactory factory = cfg.buildSessionFactory();
 Session session = factory.openSession();
 Transaction t = session.beginTransaction();
 StudentInfo studentInfo = new StudentInfo();
 t.commit(); session.close();
 System.out.println("Record Saved.."); }


Record Saved..

Related Posts

  1. Why hibernate is better than JDBC
  2. Hibernate Architecture
  3. Steps to create hibernate application in eclipse
  4. Hibernate Mapping and Configuration file
  5. Hibernate SQL Dialects List
  6. Hibernate Query Language
  7. Hibernate ID generator


Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: Sales: +91 8085506229