7:27 pm - Thursday August 17, 2017

Hibernate Oracle Using Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
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:

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

 

 

 

 

 

 

 

 

 

 

 

 

AddStudent.java:

• 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();
s.save(stu);

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

Transaction tx= s.beginTransaction();
tx.commit();

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(
							"com\\xml\\hibernate.cfg.xml")
							.buildSessionFactory();
					// sessionFactory1 = new
					// Configuration().configure("com\\xml\\student1.cfg.xml").buildSessionFactory();
				} catch (Exception e) {
					System.out.println("mathan");
					System.out.println(e.getMessage());
					System.err
							.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();
				stu.setName(name);
				stu.setDegree(degree);
				stu.setPhone(phone);
				s.save(stu);
				tx.commit();
				System.out.println("Added to oracle Database");
				if (s != null)
					s.close();

				// 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

Student.java:


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;
	}
}

Web.xml:

<?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>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

hibernate.cfg.xml:

<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"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
	        </property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
		</property>
		<property name="connection.username">
			system
		</property>
		<property name="connection.password">
			system
		</property>
		<property name="connection.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
	        </property>
		<!-- 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" />
	</session-factory>
</hibernate-configuration>

Student.hbm.xml:

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

OUTPUT:

DOWNLOAD+SOURCECODE

BACK

Filed in: Z-A Hibernate codes

Comments are closed.