Nearby lessons

2 of 19

Hibernate - Installation and Setup

📌 What You Will Learn
  • Understand Hibernate - Installation and Setup
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Installation and Setup step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Eclipse

  • Home: IBM canada [Initially]
  • Eclipse Foundations [2003]
  • website: www.eclipse.org
  • Objective: To simplify JAVA/J2EE application Development.
  • Initial Version: Ecplise3.0[2004]
  • Used Versions:Eclipse3.5,3.6,3.7[Helios,Gelilio,Indigo]
  • Latest Version: Ecplise4.2[1st march,2013][JUNO]
  • Eclipse4.3.1[26 june,2013][Kepler] ii. Eclipse4.4[9th Aug,2013][Luna] iii. Eclipse4.5[Mars] iv. Eclipse4.6[Neon]
  • Eclipse4.7[Oxygen]
  • Designed on: Java platform.
  • Type: Open Source Software.

Installation Process

  • download eclipse-jee-oxygen-1a-win32.zip
  • extract eclipse-jee-oxygen-1a-win32.zip file at the required location.
  • After step 2 , we will find "eclipse" folder ie installation folder.
  • Double click on eclipse icon available at c:\eclipse\ location.
  • After step4, we will get a window to select workspace, where specify the location where

we want to manage all the applications.

Steps To Design Hibernate Applications in Eclipse IDE

  • Prepare Java Project.
  • Prepare Hibernate Library and Add that library to Appl.
  • Prepare PJO class.
  • Prepare Mapping File.
  • Prepare Configuration File
  • Prepare Client Application.
  • Run Client Application.

Prepare Java Project

I. Right Click on "Project Exproer". II. Select "New" III. select "Java Project" IV. Provide Project Name : app1 V. Click on "Next" button. VI. Click on "Finish" button.

Add all hibernate Library to Java Application

1) Right Click on "Project". 2) Select "Properties". 3) Select "java build path". 4) Select "Library". 5) Select "Add Library". 6) Select "User Library". 7) Click on "Next" button. 8) Click on "User Libraries". 9) Click on "New" button. 10)Provide User Library Name : Hibernate3_Lib". 11)Click on "OK" button. 12)Select User Library "Hibernate3_Lib". 13)Click on "Add External Jars". 14)Select the following JAR files mandatorarily.

  • ojdbc6.jar
  • hibernate3.jar
  • jta-1.1.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • javassist-3.12.0.GA.jar
  • antlr-2.7.6.jar
  • slf4j-api-1.6.1.jar
  • hibernate-jpa-2.0-api-1.0.1.Final.jar

15)Click on "OK" button 16)Click on "Finish" button. 17)Click on "Apply" and "OK" button.

Prepare POJO class

1) Right Click on "Src" folder. 2) Select "New". 3) Select "Class". 4) Provide package Name:com.durgasoft.hbn.pojo 5) Provide class Name: Employee 6) Click on "Finish" button. 7) Declare Properties in the generated class. 8) Generate SetXXX() and getXXX() methods. a) Right Click b) Select "Source". c) Select "Generate Getters and Setters". d) Select "Select All". e) Click on "OK" button.

  • package com.durgasoft;
  • public class Employee{
  • private int eno;
  • private String ename;
  • private float esal;
  • setXXX() and getXXX()

Prepare Mapping File

1) Right Click on "Src" folder. 2) Select "New" 3) Select "Package". 4) Provide Package Name"com.durgasoft.hbn.mappings" 5) Click on "Finish".

  • Right click on "com.durgasoft.hbn.mappings" package.
  • Select "New"
  • Select "other"
  • Select "XML"
  • Select "xml file"
  • Click on "Next" button.
  • Provide File Name: Employee.hbm.xml
  • Click on "Next"
  • Click on "Next"
  • Click on "Finish" button.
  • Provide <!DOCTYPE..> tag from hibernate3.jar file under the package org.hibernate from

hibernate-mapping-3.0.dtd and past in the mapping file.

Example06
JCode Cell
1 
2<!DOCTYPE hibernate-mapping PUBLIC
3"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5<hibernate-mapping>
6<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
7<id name="eno"/>
8<property name="ename"/>
9<property name="esal"/>
10</class>
11</hibernate-mapping>
12

Prepare Configuration File

1) Right Click on "Src" folder. 2) Select "New" 3) Select "Package". 4) Provide Package Name"com.durgasoft.hbn.cfgs" 5) Click on "Finish".

  • Right click on "com.durgasoft.hbn.cfgs" package.
  • Select "New"
  • Select "other"
  • Select "XML"
  • Select "xml file"
  • Click on "Next" button.
  • Provide File Name: hibernate.cfg.xml
  • Click on "Next"
  • Click on "Next"
  • Click on "Finish" button.
  • provide <!DOCTYPE...> tag from hibernate3.jar file from org.hibernate package from

hibernate-configuration-3.0.dtd.

Example07
JCode Cell
1 
2<!DOCTYPE hibernate-configuration PUBLIC
3"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5<hibernate-configuration>
6<session-factory>
7<property name="connection.driver_class">
8oracle.jdbc.OracleDriver
9</property>
10<property name="connection.url">
11jdbc:oracle:thin:@localhost:1521:xe
12</property>
13<property name="connection.user">system</property>
14<property name="connection.password">durga</property>
15<property name="hibernate.dialect">
16org.hibernate.dialect.OracleDialect
17</property>
18<mapping resources="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
19</session-factory>
20</hibernate-configuration>
21

Prepare Client App

1) Right click on src 2) Select "New" button. 3) Select "Class". 4) Provide package Name: com.durgasoft.hbn.test 5) Provide Class Name : ClientApp 6) Select "select public static void main(String[] args)" check box. 7) Proivide Application logic inside main() mnethod.

Example08
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.Transaction;
7import org.hibernate.cfg.Configuration;
8 
9import com.durgasoft.hbn.pojo.Employee;
10 
11public class ClientApp {
12 
13public static void main(String[] args)throws Exception {
14Configuration cfg = new Configuration();
15cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
16SessionFactory session_Factory = cfg.buildSessionFactory();
17Session session = session_Factory.openSession();
18Transaction tx = session.beginTransaction();
19Employee emp = new Employee();
20emp.setEno(111);
21emp.setEname("AAA");
22emp.setEsal(5000);
23emp.setEaddr("Hyd");
24session.save(emp);
25tx.commit();
26System.out.println("Employee Record inserted Succesfully");
27session.close();
28session_Factory.close();
29}
30}
31

Run the application — Employee.java

  • Right click on ClientApp.java
  • Select "Run As".
  • Select "Java Application".

Note: Download JAR files from https://sourceforge.net/projects/hibernate/files/hibernate3 Hibernate Applications

Example:

Example09
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3public class Employee {
4private int eno;
5private String ename;
6private float esal;
7private String eaddr;
8setXXX() and getXXX()
9}
10

Run the application — Employee.hbm.xml

Example10
JCode Cell
1 
2<!DOCTYPE .... >
3<hibernate-mapping>
4<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
5<id name="eno" column="eno"/>
6<property name="ename"/>
7<property name="esal"/>
8<property name="eaddr"/>
9</class>
10</hibernate-mapping>
11

Run the application — hibernate.cfg.xml

Example11
JCode Cell
1 
2<!DOCTYPE...>
3<hibernate-configuration>
4<session-factory>
5<property name="connection.driver_Class"> oracle.jdbc.OracleDriver</property>
6<property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
7<property name="connection.username">system</property>
8<property name="connection.password">durga</property>
9<property hibernate.dialect"> org.hibernate.dialect.OracleDialect</property>
10<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
11</session-factory>
12</hibernate-configuration>
13

Run the application — ClientApp.java

Example12
JCode Cell
1 
2package com.durgasoft.hbn.test;
3import org.hibernate.*;
4import org.hibernate.cfg.Configuration;
5import com.durgasoft.hbn.pojo.Employee;
6public class ClientApp {
7public static void main(String[] args)throws Exception {
8Configuration cfg = new Configuration();
9cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
10SessionFactory session_Factory = cfg.buildSessionFactory();
11Session session = session_Factory.openSession();
12Transaction tx = session.beginTransaction();
13Employee emp = new Employee();
14emp.setEno(111);
15emp.setEname("AAA");
16emp.setEsal(5000);
17emp.setEaddr("Hyd");
18session.save(emp);
19tx.commit();
20System.out.println("Employee Record inserted Succesfully");
21session.close();
22session_Factory.close();
23}
24}
25

Run the application

In Hibernate Applications, when we access persistence methods , Hibernate Software will take that persistence method call and it will prepare databse dependent native sql query on the basis of hibername mapping details and configuration details . In this context, to display the generated database dependent sql query on console we have to use "show_sql" property with the value "true" in hibernate configuration file.

Example On Hibernate SaveOrUpdate(--) method — Employee.java

Example14
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34 
35}
36

Example On Hibernate SaveOrUpdate(--) method — Employee.hbm.xml

Example15
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8 <id name="eno"/>
9 <property name="ename"/>
10 <property name="esal"/>
11<property name="eaddr"/>
12</class>
13</hibernate-mapping>
14

Example On Hibernate SaveOrUpdate(--) method — hibernate.cfg.xml

Example16
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7<session-factory>
8 <property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
9 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10 <property name="connection.user">system</property>
11<property name="connection.password">durga</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Example On Hibernate SaveOrUpdate(--) method — ClientApp.java

Example17
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.Transaction;
7import org.hibernate.cfg.Configuration;
8 
9import com.durgasoft.hbn.pojo.Employee;
10 
11public class ClientApp {
12 
13public static void main(String[] args) {
14SessionFactory session_Factory = null;
15Session session = null;
16try {
17 Configuration cfg = new Configuration();
18 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
19 session_Factory = cfg.buildSessionFactory();
20 session = session_Factory.openSession();
21 Transaction tx = session.getTransaction();
22 tx.begin();
23 Employee emp = new Employee();
24 emp.setEno(111);
25 emp.setEname("XXX");
26 emp.setEsal(9000);
27 emp.setEaddr("Sec");
28 session.saveOrUpdate(emp);
29 tx.commit();
30 System.out.println("Employee Updated Successfully");
31} catch (Exception e) {
32 e.printStackTrace();
33}finally {
34 session.close();
35 session_Factory.close();
36}
37 
38}
39}
40

Example to delete records from DB — Employee.java

Example18
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34 
35}
36

Example to delete records from DB — Employee.hbm.xml

Example19
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8 <id name="eno"/>
9 <property name="ename"/>
10 <property name="esal"/>
11<property name="eaddr"/>
12</class>
13</hibernate-mapping>
14

Example to delete records from DB — hibernate.cfg.xml

Example20
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7<session-factory>
8<property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
9<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10<property name="connection.user">system</property>
11<property name="connection.password">durga</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Example to delete records from DB — ClientApp.java

Example21
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.Transaction;
7import org.hibernate.cfg.Configuration;
8 
9import com.durgasoft.hbn.pojo.Employee;
10 
11public class ClientApp {
12 
13public static void main(String[] args) {
14SessionFactory session_Factory = null;
15Session session = null;
16try {
17 Configuration cfg = new Configuration();
18 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
19 session_Factory = cfg.buildSessionFactory();
20 session = session_Factory.openSession();
21 Transaction tx = session.beginTransaction();
22 Employee emp = new Employee();
23 emp.setEno(111);
24 session.delete(emp);
25 tx.commit();
26 System.out.println("Employee Deleted Successfully");
27} catch (Exception e) {
28 e.printStackTrace();
29}finally {
30 session.close();
31 session_Factory.close();
32}
33 
34}
35 
36}
37

Example To retrieve Record from DB — Employee.java

Example22
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34 
35}
36

Example To retrieve Record from DB — Employee.hbm.xml

Example23
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8 <id name="eno"/>
9 <property name="ename"/>
10 <property name="esal"/>
11<property name="eaddr"/>
12</class>
13</hibernate-mapping>
14

Example To retrieve Record from DB — hibernate.cfg.xml

Example24
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7<session-factory>
8<property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
9<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
10<property name="connection.user">system</property>
11<property name="connection.password">durga</property>
12<property name="show_sql">true</property>
13<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
14<mapping resource="com/durgasoft/hbn/mappings/Employee.hbm.xml"/>
15</session-factory>
16</hibernate-configuration>
17

Example To retrieve Record from DB — ClientApp.java

Example25
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.cfg.Configuration;
7 
8import com.durgasoft.hbn.pojo.Employee;
9 
10public class ClientApp {
11 
12public static void main(String[] args) {
13SessionFactory session_Factory = null;
14Session session = null;
15 
16try {
17 Configuration cfg = new Configuration();
18 cfg.configure("/com/durgasoft/hbn/cfgs/hibernate.cfg.xml");
19 session_Factory = cfg.buildSessionFactory();
20 session = session_Factory.openSession();
21 Employee emp = (Employee)session.get("com.durgasoft.hbn.pojo.Employee",222 );
22 if(emp == null) {
23 System.out.println("Employee Not Existed");
24 }else {
25 System.out.println("Employee Details");
26 System.out.println("---------------------");
27 System.out.println("Employee Number :"+emp.getEno());
28 System.out.println("Employee Name :"+emp.getEname());
29 System.out.println("Employee Salary :"+emp.getEsal());
30 System.out.println("Employee Address :"+emp.getEaddr());
31 }
32
📝 Key Takeaways
  • Key ideas of Hibernate - Installation and Setup explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5