Home > Hibernate > Hibernate Application Saving Object

Hibernate Application Saving Object

Hibernate Application Saving Object

Hibernate Application Saving Object is the basic Hibernate application which saved data as object format into Oracle database.

Hibernate is a Java framework that simplifies the development of Java application to interact with the database.

It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence.


Hibernate Application Saving Object required jar and software mentioned below:

  1. JDK 1.8
  2. Eclipse IDE
  3. Download Hibernate Jars : https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.3.2.Final/hibernate-release-5.3.2.Final.zip/download –>hibernate-release-5.3.2.Final\hibernate-release-5.3.2.Final\lib\required\all jar
  4. Oracle jar: C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar

These are below mentioned files required to create Hibernate Application Saving Object:

  1. POJO(Plain Old Java Object) class ( with setters and gettrs method): STD.java
  2. Hibernate mapping file: STD.hbm.xml
  3. Hibernate Configuration file and file name is always same as: hibernate.cfg.xml
  4. Hibernate main class: SaveObjectTest.java

STD.java

package com.ravi.domain;

public class STD {
private int sid;
private String sname;
private int marks;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}

STD.hbm.xml

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.ravi.domain.STD" table="std">

<id name="sid" column="sid"/>
<property name="sname" column="sname"/>
<property name="marks" column="marks"/>

</class>

</hibernate-mapping>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">tiger</property>

<mapping resource="com/ravi/cfgs/STD.hbm.xml"/>

</session-factory>

</hibernate-configuration>


SaveObjectTest.java

package com.ravi.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.ravi.domain.STD;

public class SaveObjectTest {


public static void main(String[] args) {

Configuration cfg=null;
SessionFactory factory=null;
Session session =null;
Transaction tx=null;
STD s=null;

//Activate hibernate framework (Bootstrap HB)
cfg=new Configuration();

//read both mapping file and CFG file
cfg=cfg.configure("/com/ravi/cfgs/hibernate.cfg.xml");

//Build sessionfactory object
factory=cfg.buildSessionFactory();

//open session with DB software
session=factory.openSession();

//Create object for domain class

s=new STD();
s.setSid(2);
s.setSname("Ranjan");
s.setMarks(356);

try{
tx=session.beginTransaction();
session.save(s);
tx.commit();
System.out.println("Object is saved");
}
catch(Exception e)
{
tx.rollback();
}
session.close();
factory.close();

}

}

You may also like :-
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *