Hibernate Annotations example

Hibernate Application using annotations

The hibernate application can be created using annotations. There are many annotations that can be used to create hibernate application such as @Entity, @Table, @Id, @Column etc.

This example is the same as the first example except that it uses annotations. There we first started by creating the .hbm.xml file, here there is no need to create it instead we will use annotations to do the object relational mapping.

Employee.java

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

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <mapping class="com.ashok.hibernate.annotations.model.Employee"/>
   </session-factory>
</hibernate-configuration>

ClientApp.java

package com.ashok.hibernate.annotations;

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

import com.ashok.hibernate.annotations.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.configure("/com/ashok/hibernate/select/hibernate.cfg.xml");
			sessionFactory = cfg.buildSessionFactory();
			session = sessionFactory.openSession();
			Employee emp = (Employee)session.get("com.ashok.hibernate.annotations.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
Hibernate Annotations example
Scroll to top