Você está na página 1de 19

HIBERNATE TECHNOLOGY

By: Tushar gupta B.Tech(CSE) GTbit

Hibernate Technology
Hibernate is an Object-Relational Mapping (ORM) solution for JAVA. It maps the Java classes to the database tables. It also provides the data query and retrieval facilities that significantly reduce the development time. It is a powerful, high performance object/relational persistence and query service.

Features
Hibernate Technology

Provides three full-featured query facilities: a) Hibernate Query Language b) Hibernate Criteria Query API c) Support for query in native SQL Dialect Takes less Development time Supports Automatic Key generation XML Binding Has ECLIPSE support

Architecture

Components of Architecture
Connection
Transaction

Management
Management Mapping

Object-Relational

Connection Management
Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.

Transaction Management
Transaction management service provide the ability to the user to execute more than one database statements at a time.

Object-Relational Mapping
Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query.

Hibernate framework objects


SessionFactory Maintains a threadsafe (immutable) cache of compiled mappings for a single database Typically one for each database Session The life of a Session is bounded by the beginning and end of a logical transaction A session represents a persistence

Hibernate framework objects


Persistent objects and collections Short-lived, single threaded objects containing persistent state and business function Changes made to persistent objects are reflected to the database tables (when they are committed) Transaction A single-threaded, short-lived object used by the

Hibernate framework objects


Transient and Detached objects Instances of persistent classes that are not currently associated with a Session, thus without a persistent context Changes made to transient and detached objects do not get reflected to the database table

How it works?
Hibernate: 1) itself opens connection to database, 2) converts HQL (Hibernate Query Language) statements to database specific statement, 3) receives result set, 4) then performs mapping of these database specific data to Java objects which are directly used by Java application.

Hibernate Configuration
Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment.
Code:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration "http://hibernate.sourceforge.net/hibernateconfiguration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/hibernatetutorial</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

Persistence Classes
Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column
Code:
package com.app.test; public class NewEmp {private String firstName; private String email; public String getEmail() { return email; } public void setEmail(String string) { email = string; } public String getFirstName() { return firstName; public void setFirstName(String string) { firstName = string; }

Object to Table Mapping


The file .hbm.xml is used to map Object to the table in the database. Code:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.time.manage.app.beans"> <class name="redirectTsBean" table="ts_attts_dtls"> <id name="logOnTime" type="long"> <column name="LoginTime" /> <generator class="assigned" /> </id> <property name="halfDay" type="string"> <column name="HalfDay" length="45"></column> </property> </class></hibernate-mapping>

Advantages over JDBC


Relational

Persistence for JAVA Transparent Persistence Database Dependent Code Support for Query Language Optimize Performance with Caching Easily Scalable

Disadvantages
Steep

learning curve. Use of Hibernate is an overhead for the applications which are : simple and use one database that never change need to put data to database tables, no further SQL queries there are no objects which are mapped to two different tables Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple

References

http://www.javapassion.com/j2ee/hiberna tebasics.pdf http://www.mindfiresolutions.com/mindfir e/Java_Hibernate_JDBC.pdf http://www.roseindia.net/hibernate/


e-book : Java Persistence with

THANK YOU!!

Você também pode gostar