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 “hibernate.properties” under “src” folder .

In this context, when we create Configuration class object , Hibernate Software will search for the properties file with the name “hibernate.properties” 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 hibernate.properties to some other abc.properties 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.properties

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

employee.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<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" />
	</class>
</hibernate-mapping>

Employee.java

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() {
		super();
	}
	
	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;
	}
}

ClientApp.java

package com.ashok.hibernate.declarativeapproach;

import java.io.File;
import java.io.FileInputStream;
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/hibernate.properties";

	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();
			p.load(fis);
			cfg.setProperties(p);
			cfg.addResource("employee.hbm.xml");
			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("---------------------------");
				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) {
			e.printStackTrace();
		} finally {
			if (null != session) {
				session.close();
			}
			if (null != sessionFactory) {
				sessionFactory.close();
			}
		}
	}
}

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