3:30 pm - Saturday January 21, 2017

Hibernate Oracle Using Eclipse

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

• This explains Oracle data base in hibernate.

Project structure:


























• Here we will use sessionFactory for create connetion Oracle database.

sessionFactory = new Configuration().configure(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

• Then we will create objects for pojo classes and then save the database using session.

Student stu=new Student();

• Then we will use Transaction statement for begin and commit the database.

Transaction tx= s.beginTransaction();

package com.candidjava;

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

public class AddStudent {
	private static SessionFactory sessionFactory1;
	private static SessionFactory sessionFactory;

	public static void main(String args[]) throws Exception {
		if (args[0] != null || args[1] != null || args[2] != null) {// begin if
																	// A
			String name = args[0];
			String name1 = args[0];
			String degree = args[1];
			String phone = args[2];

			System.out.println("Name: " + name);
			System.out.println("Degree: " + degree);
			System.out.println("Phone: " + phone);

			if ((name.equals("") || degree.equals("") || phone.equals(""))) {
				System.out.println("All informations are Required");
			} else {

				try {// begin try
					sessionFactory = new Configuration().configure(
					// sessionFactory1 = new
					// Configuration().configure("com\\xml\\student1.cfg.xml").buildSessionFactory();
				} catch (Exception e) {
							.println("Initial SessionFactory creation failed."
									+ e);

				Session s = sessionFactory.openSession();
				// Session s1 =sessionFactory1.openSession();
				// Transaction tx1= s1.beginTransaction();
				Transaction tx = s.beginTransaction();
				Student stu = new Student();
				System.out.println("Added to oracle Database");
				if (s != null)

				// Student1 stu1=new Student1();
				// stu1.setName(name1);
				// s1.save(stu1);
				// tx1.commit();
				// System.out.println("Added to mysql Database");
				// if (s1 != null)
				// s1.close();
		}// end of if A
	}// end of method
}// end of class


package com.candidjava;

public class Student {
	private long id;
	private String name;
	private String degree;
	private String phone;

	public long getId() {
		return id;

	public String getName() {
		return name;

	public String getDegree() {
		return degree;

	public String getPhone() {
		return phone;

	public void setId(long String) {
		id = String;

	public void setName(String string) {
		name = string;

	public void setDegree(String string) {
		degree = string;

	public void setPhone(String string) {
		phone = string;

	public String toString() {
		return name;


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Oracle database</display-name>


<session-factory name=”studentFactory”>

Provides a unique name for property used with this database.

<property name=”connection.driver_class”> oracle.jdbc.OracleDriver </property>

The property connection.driver_class is used for loading database driver just like loading driver in JDBC(Class.forName();)

<property name=”connection.url”> jdbc:oracle:thin:@localhost:1521:XE </property>

Connection.url property is used to load connection for the paticular database

<property name=”connection.username”> system </property>

This property implies user name of the database.
“system”—This is default user name for oracle.

<property name=”connection.password”> system </property>

This property implies the database password.
system –Replace this with your oracle password.

<property name=”connection.pool_size”>5</property>

Connection.pool_size property is used to maintain connection pooling by application server not by the database server

<property name=”dialect”> org.hibernate.dialect.OracleDialect </property>

This is an optional property. which generates sql query for database based on the dialect we used. Different database have different query. Query may be change as the database changes. to over come this problem dialect come in exist.

<property name=”show_sql”>true</property>

This property displays sql query in console generated by the previous property dialect.
If it is false it doesn’t display the query.

<property name=”hbm2ddl.auto”>update</property>

Update–This Property Used to update the table Rather than create the new table.
If table already exists it update values into table otherwise it creates new table automatically.
Create–This property creates new table.
If the table already exists, it deletes all records in the table and create new table with the given values.
It’s better use update property rather than create to maintain old records.

<mapping resource=”com\\xml\\Student.hbm.xml”/>

Mapping Resource property is used for mapping pojo with table.
Be sure while com\\xml matches your package name else give your package name instead.

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD//EN"
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
		<property name="connection.url">
		<property name="connection.username">
		<property name="connection.password">
		<property name="connection.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="com\\xml\\Student.hbm.xml" />


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	<class name="com.candidjava.Student" table="studentOracle">
		<id name="id" type="long" column="ID">
			<generator class="increment" />
		<property name="name" column="name" not-null="true" />
		<property name="degree" column="degree" />
		<property name="phone" column="phone" />




Filed in: Z-A Hibernate codes

Comments are closed.