Hibernate Framework: Traditional Way of Database Connectivity

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Hibernate Framework

Traditional Way of Database Connectivity


1. Declaration of Resource Class.forName(sun.jdbc.odbc.JdbcOdbcDriver) 2. Connecting to database Connection con=DriverManager.getConnection(jdbc:odbc:dns,un,pwd) 3. Disable auto commit mode. a. We may perform multiple database operations con.setAutoCommit(false); 4. Preparing Query PreparedStatement ps=con.prepareStatement(insert into Student values (?,?,?)) ps.setString(1,A11); ps.setString(2,Arjun); ps.setInt(3,552); ps.execute(); 5. Connection committing con.commit(); 6. Connection roll backing.. con.rollback(); ***These are not the responsibilities of the Developer to perform all these tasks

Improvements with Hibernate Frame Work


All the Database connection statements and Driver Class are loaded in Configuration file. (hibernate.cfg.xml is default hibernate configuration file) We are going to use RowSet rather than ResultSet We going to perform Batch Operation in order reduce databases hits Connection Pooling for fast execution

Advantage
Developer effort reduced by 95% 1. Project Development Cost Reduced 2. Improves the performance by using best concepts of JDBC such as PreparedStatement, Batch Operation, RowSets, Connection Pooling 3. Managing Associates in Hibernate Framework is as simple as working with collection api such as List, Set, Map 4. No memory leaks in Hibernate framework since resources are released by hibernate framework 5. Hibernate provides ORM framework. So, It has to follow all ORM Rules a. Objects becomes Records and vice versa b. Support Object Query Language (HQL) c. Should Support different primary key generators d. Should configure Hash memory called 2nd level Cache 6. Hibernate used POJO (javabeans without events).

Plain Old Java Objects (POJO)


POJO is container which holds database data temporarily so that both presentation layer and controller layer access POJOs. 1. 2. 3. 4. 5. Public class should be defined as it accessed from Different Packages. Should inherit Serzable interface in order to access the class out of the JVM Declare properties with private modifiers to become tight encapsulation Define setters and Getters of every property of Bean Define public no-arg constructor

import java.io.Serializable; @SuppressWarnings("serial") public class Employee implements Serializable { private String empname; private Integer empage; private Double empsalary; private Integer empid; public Employee(String empname, Integer empage, Double empsalary, Integer empid) { this.empname = empname;

this.empage = empage; this.empsalary = empsalary; this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public Integer getEmpage() { return empage; } public void setEmpage(Integer empage) { this.empage = empage; } public Double getEmpsalary() { return empsalary; } public void setEmpsalary(Double empsalary) { this.empsalary = empsalary; } public Integer getEmpid() { return empid; } public void setEmpid(Integer empid) { this.empid = empid; } @Override public String toString() { return "Employee [empname=" + empname + ", empage=" + empage + ", empsalary=" + empsalary + ", empid=" + empid + "]"; } }

Hibernate Architecture
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <hibernate-configuatrion> <property name="driverclassname">sun.jdbc.odbc.JdbcOdbcDriver</property> <property name="url">jdbc:odbc:hib</property> <property name="username">username</property> <property name="password">password</property> </hibernate-configuatrion>

You might also like