Showing posts with label Hibernate concepts. Show all posts
Showing posts with label Hibernate concepts. Show all posts

Wednesday, June 5, 2013

Sample Hibernate Application Using Eclipse and using annotations

In first example, I have created sample Hibernate project using mapping files
Next step is to convert same application to use annotations.
Change Employee.java and Department.java files to use annotations as shown below.

Employee.java

package com.test.beans;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="EMP")
public class Employee {
@Id @GeneratedValue
@Column(name="emp_id")
private Long empId;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="degn")
private String designation;
@Column(name="sal")
private double salary;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="dept")
private Department dept;
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}

Department.java:

package com.test.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="DEPT")
public class Department {
@Id @GeneratedValue
@Column(name="dept_id")
private long deptId;
@Column(name="dept_name")
private String deptName;
@Column(name="loc")
private String location;
public long getDeptId() {
return deptId;
}
public void setDeptId(long deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}

Change hibernate.cfg.xml file as:

  <? xml version="1.0" encoding="UTF-8"?>


<!

DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD/EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >


<hibernate-configuration>



<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>

<mapping class="com.test.beans.Employee" />
<mapping class="com.test.beans.Department" /> 
</session-factory>
</hibernate-configuration>


You can delete (organization.hbm.xml) hbm mapping file.

Now your sample annotation project is ready. You can run Test.java file to check the output.

Sunday, June 2, 2013

Automatic Dirty checking and Transaction write-behind concepts of Hibernate

Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is called transaction write-behind.

Automatic Dirty checkingIf object which is already saved is modified, then Hibernate automatically call update on that object.
Hibernate is capable of detecting all objects that are modified or are dirty. It is done with the help of PersistanceContext.
Within PersistanceContext, Hibernate has copy of all persistent objects that are loaded from database in Session. It compares persistent object to determine whether object is modified.


Hibernate allows implementation of custom dirty checking algorithm. This is done by implementing findDirty() method of org.hibernate.Interceptor interface for a Session.