Nearby lessons

7 of 19

Hibernate - Configuration Approaches

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

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

ANS

Yes, it is possible to run Hibernate applications without using configuration file.

In Hibernate Applications, we are able to provide configuration details in the following two

approaches.

ANS

a) By using properties file

b) By Using XML file

Example02
JCode Cell
1 
2Programmatic Approach
3Declarative Approach
4

ANS

In Programmatic approach, to provide configuration details, first we have to create Configuration

class object then we have to set all hibernate properties to Configuration class object explicitly by

using the following method.

public void setProperty(String prop_Name, String prop_Val)

To add mapping file name and location to Configuration file we have to use the following method.

public void addResource(String mapping_File_Name)

Note: If we are using annotations in place of mapping file then we have to use the following

method to add annotated class.

public void addAnnotatedClass(Class class)

Example on without configuration File and with mapping file in Programmatic Approach:

Employee.java

Example03
JCode Cell
1 
2Programmatic Approach
3

ANS

Example04
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3

ANS

Example05
JCode Cell
1 
2public class Employee {
3private int eno;
4private String ename;
5private float esal;
6private String eaddr;
7

ANS

Example06
JCode Cell
1 
2public int getEno() {
3return eno;
4}
5public void setEno(int eno) {
6this.eno = eno;
7}
8public String getEname() {
9return ename;
10}
11public void setEname(String ename) {
12this.ename = ename;
13}
14public float getEsal() {
15return esal;
16}
17public void setEsal(float esal) {
18this.esal = esal;
19}
20public String getEaddr() {
21return eaddr;
22}
23public void setEaddr(String eaddr) {
24this.eaddr = eaddr;
25}
26

ANS

Employee.hbm.xml

Example07
JCode Cell
1 
2}
3

ANS

ClientApp.java

Example08
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" column="ENO"/>
9<property name="ename" column="ENAME"/>
10<property name="esal" column="ESAL"/>
11<property name="eaddr" column="EADDR"/>
12</class>
13</hibernate-mapping>
14

ANS

Example09
JCode Cell
1 
2package com.durgasoft.hbn.test;
3

ANS

Example10
JCode Cell
1 
2import org.hibernate.Session;
3import org.hibernate.SessionFactory;
4import org.hibernate.cfg.AnnotationConfiguration;
5import org.hibernate.cfg.Configuration;
6

ANS

Example11
JCode Cell
1 
2import com.durgasoft.hbn.pojo.Employee;
3

ANS

Example12
JCode Cell
1 
2public class ClientApp {
3

ANS

Example13
JCode Cell
1 
2public static void main(String[] args) {
3SessionFactory sessionFactory = null;
4Session session = null;
5try {
6// Configuration cfg = new AnnotationConfiguration();
7Configuration cfg = new Configuration();
8cfg.setProperty("hibernate.connection.driver_Class","oracle.jdbc.OracleDriver");
9cfg.setProperty("hibernate.connection.url","jdbc:oracle:thin:@localhost:1521:xe");
10cfg.setProperty("hibernate.connection.username", "system");
11cfg.setProperty("hibernate.connection.password", "durga");
12cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
13cfg.addResource("Employee.hbm.xml");
14//cfg.addAnnotatedClass(com.durgasoft.hbn.pojo.Employee.class);
15sessionFactory = cfg.buildSessionFactory();
16session = sessionFactory.openSession();
17Employee emp = (Employee) session.get(Employee.class, 222);
18if (emp == null) {
19System.out.println("Employee Not Existed");
20} else {
21System.out.println("Employee Details");
22System.out.println("====================");
23System.out.println("Employee Number :" + emp.getEno());
24System.out.println("Employee Name :" + emp.getEname());
25System.out.println("Employee Salary :" + emp.getEsal());
26System.out.println("Employee Address :" + emp.getEaddr());
27}
28} catch (Exception e) {
29e.printStackTrace();
30} finally {
31

ANS

Example14
JCode Cell
1 
2session.close();
3sessionFactory.close();
4

ANS

Example on without configuration file and without mapping file in Programmatic approach:

Employee.java

Example15
JCode Cell
1 
2}
3}
4}
5

ANS

Example16
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3

ANS

Example17
JCode Cell
1 
2import javax.persistence.Column;
3import javax.persistence.Entity;
4import javax.persistence.Id;
5import javax.persistence.Table;
6

ANS

Example18
JCode Cell
1 
2@Entity
3@Table(name="emp1")
4public class Employee {
5@Id
6@Column(name="ENO")
7private int eno;
8@Column(name="ENAME")
9private String ename;
10@Column(name="ESAL")
11private float esal;
12@Column(name="EADDR")
13private String eaddr;
14

ANS

Example19
JCode Cell
1 
2public int getEno() {
3return eno;
4}
5public void setEno(int eno) {
6this.eno = eno;
7}
8public String getEname() {
9return ename;
10}
11public void setEname(String ename) {
12this.ename = ename;
13}
14public float getEsal() {
15return esal;
16}
17public void setEsal(float esal) {
18this.esal = esal;
19}
20public String getEaddr() {
21return eaddr;
22}
23public void setEaddr(String eaddr) {
24this.eaddr = eaddr;
25}
26

ANS

ClientApp.java

Example20
JCode Cell
1 
2}
3

ANS

Example21
JCode Cell
1 
2package com.durgasoft.hbn.test;
3

ANS

Example22
JCode Cell
1 
2import org.hibernate.Session;
3import org.hibernate.SessionFactory;
4import org.hibernate.cfg.AnnotationConfiguration;
5import org.hibernate.cfg.Configuration;
6

ANS

Example23
JCode Cell
1 
2import com.durgasoft.hbn.pojo.Employee;
3

ANS

Example24
JCode Cell
1 
2public class ClientApp {
3

ANS

Example25
JCode Cell
1 
2public static void main(String[] args) {
3SessionFactory sessionFactory = null;
4Session session = null;
5try {
6// Configuration cfg = new AnnotationConfiguration();
7Configuration cfg = new Configuration();
8cfg.setProperty("hibernate.connection.driver_Class","oracle.jdbc.OracleDriver");
9cfg.setProperty("hibernate.connection.url","jdbc:oracle:thin:@localhost:1521:xe");
10cfg.setProperty("hibernate.connection.username", "system");
11cfg.setProperty("hibernate.connection.password", "durga");
12cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
13//cfg.addResource("Employee.hbm.xml");
14cfg.addAnnotatedClass(com.durgasoft.hbn.pojo.Employee.class);
15sessionFactory = cfg.buildSessionFactory();
16session = sessionFactory.openSession();
17Employee emp = (Employee) session.get(Employee.class, 222);
18if (emp == null) {
19System.out.println("Employee Not Existed");
20} else {
21System.out.println("Employee Details");
22System.out.println("====================");
23System.out.println("Employee Number :" + emp.getEno());
24System.out.println("Employee Name :" + emp.getEname());
25System.out.println("Employee Salary :" + emp.getEsal());
26System.out.println("Employee Address :" + emp.getEaddr());
27}
28} catch (Exception e) {
29e.printStackTrace();
30} finally {
31session.close();
32sessionFactory.close();
33

ANS

Example26
JCode Cell
1 
2}
3

ANS

In Programatic approach, if we want to change configuration details like connection properties or

database properties,.... then we have to perform modifications in JAVA code, if we perform

modifications in JAVA code then we must recompile the java application , it is not suggestible in

enterprise applications. To overcome the problem we have to use Declarative approach.

Example27
JCode Cell
1 
2}
3}
4

2) Declarative approach

In Declarative approach, we will provide all configuration details in either properties file or in XML

file then we will send configuration details to Hibernate application.

There are two ways to provide configuration details to Hibernate Applications in Declarative

Approach.

2) Declarative approach

Example29
JCode Cell
1 
2By using properties file.
3By Using XML file.
4
📝 Key Takeaways
  • Key ideas of Hibernate - Configuration Approaches explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3