Programmatic Approach

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.

  1. Programmatic Approach
  2. Declarative Approach
    1. By using properties file
    2. By Using XML file
Programmatic Approach

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 propName, String propVal)

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

public void addResource(String mappingFileName)

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

package com.ashok.hibernate.programmaticapproch.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;
	}
}

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.programmaticapproch.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>

ClientApp.java

package com.ashok.hibernate.programmaticapproch;

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

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

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {

		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.setProperty("hibernate.connection.driver_Class", "oracle.jdbc.OracleDriver");
			cfg.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:xe");
			cfg.setProperty("hibernate.connection.username", "system");
			cfg.setProperty("hibernate.connection.password", "ashok");
			cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
			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();
			}
		}
	}
}

Example on without configuration file and without mapping file

package com.ashok.hibernate.programmaticapproch.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 
 * @author ashok.mariyala
 *
 */
@Entity
@Table(name = "emp")
public class Employee {
	@Id
	@Column(name = "emp_id")
	private String empId;
	@Column(name = "emp_name")
	private String empName;
	@Column(name = "address")
	private String address;
	@Column(name = "salary")
	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.programmaticapproch;

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

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

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {

		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.setProperty("hibernate.connection.driver_Class", "oracle.jdbc.OracleDriver");
			cfg.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:xe");
			cfg.setProperty("hibernate.connection.username", "system");
			cfg.setProperty("hibernate.connection.password", "ashok");
			cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
			cfg.addAnnotatedClass(com.ashok.hibernate.programmaticapproch.model.Employee.class);
			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();
			}
		}
	}
}

Output

Employee Details
---------------------------
Employee ID : E0087
Employee Name : Ashok Kumar
Employee Salary : 75000
Employee Address : Hyderabad

In Programmatic approach, if we want to change configuration details like connection properties or database properties etc 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.

Programmatic Approach


Scroll to top