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.

  1. By using properties file
  2. By Using XML file
Using properties file

In this approach, to provide all hibernate configuration details, we have to declare a properties file with the default name “” under “src” folder .

In this context, when we create Configuration class object , Hibernate Software will search for the properties file with the name “” and get all the configuration details into Configuration class object.

Here we must add mapping file explicitly to Configuration File by using the following method.

public void addResource(String mappingFileName)

If we change name and location of properties file from to some other then we have to give that intimation to Hibernate software, for this, we have to create FileInputStream and Properties class object then we have to set Properties object to Configuration class object by using the following method.

public void setProperties(Properties p)

hibernate.connection.driver_Class = oracle.jdbc.OracleDriver
hibernate.connection.url = jdbc:oracle:thin:@localhost:1521:xe
hibernate.connection.username = system
hibernate.connection.password = ashok
hibernate.dialect = org.hibernate.dialect.OracleDialect


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"

	<class name="com.ashok.hibernate.declarativeapproch.model.Employee" table="emp">
		<id name="empId" column="emp_id" type="string" />
		<property name="empName" column="emp_name" type="string" />
		<property name="address" column="address" type="string" />
		<property name="salary" column="salary" type="double" />

package com.ashok.hibernate.declarativeapproch.model;

 * @author ashok.mariyala
public class Employee {
	private String empId;
	private String empName;
	private String address;
	private double salary;
	public Employee() {
	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;
	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String getAddress() {
		return address;

	public void setAddress(String address) {
		this.address = address;

	public double getSalary() {
		return salary;

	public void setSalary(double salary) {
		this.salary = salary;

package com.ashok.hibernate.declarativeapproach;

import java.util.Properties;

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

import com.ashok.hibernate.insert.model.Employee;

 * @author ashok.mariyala
public class ClientApp {
	private static final String PATH = "/home/ashok/work/ashok/src/main/java/com/ashok/hibernate/declarativeapproach/";

	public static void main(String[] args) throws Exception {
		SessionFactory sessionFactory = null;
		Session session = null;
		try (FileInputStream fis = new FileInputStream(new File(PATH))) {
			Configuration cfg = new Configuration();
			Properties p = new Properties();
			sessionFactory = cfg.buildSessionFactory();
			session = sessionFactory.openSession();
			Employee emp = (Employee) session.get("com.ashok.hibernate.programmaticapproch.model.Employee", "E0087");

			if (emp == null) {
				System.out.println("Employee Not Existed");
			} else {
				System.out.println("Employee Details");
				System.out.println("Employee ID :" + emp.getEmpId());
				System.out.println("Employee Name :" + emp.getEmpName());
				System.out.println("Employee Salary :" + emp.getSalary());
				System.out.println("Employee Address :" + emp.getAddress());
		} catch (Exception e) {
		} finally {
			if (null != session) {
			if (null != sessionFactory) {

In hibernate applications, if we use “properties” file then we are able to provide only hibernate connection properties, dialect properties. etc. but, we are unable to provide mapping properties,… through properties file, to provide mapping properties we have to use java methods like addResource(–) or addAnnotatedClass(–) methods from Configuration class.

In Hibernate applications, if we want to provide all configuration details in declarative manner then we have to use XML file approach.

If we want to use XML file to provide all configuration details then we have to use configure() method to get all configuration details from XML file. Here configure() method will search for XML file with the default name hibernate.cfg.xml under src folder in order to get configuration details. If we change the default name and location of hibernate configuration file then we have to pass that name and location to configure(–) method as parameter.

Declarative approach

Scroll to top