Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
Free Online Hibernate Tutorials,Hibernate Programming Tutorials,Hibernate notes
GeekMantra
Search Site

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2
J2SE [Java 2 Standard Edition]
Core Java Introduction
Exception Handling
Collection & Util Classes
Multithreading
Input & Output Streams
Applet
Adwance Windows Toolkit (AWT)
Swings (Adwance GUI)
Event Handling
Java Networking
Remote Method Invocation (RMI)
Java Database Connectivity (JDBC)
J2EE [Java 2 Enterprise Edition]
Java 2 Enterprise Edition Intoduction (J2EE)
Server Side Programming (Servlet)
Java Server Pages (JSP)
Enterprise Java Bean
MVC Framework (Struts)
Advance Framework (Springs)
Object-relational mapping (Hibernate)
eXtensible Markup Language (XML)
Asynchronous JavaScript and XML (AJAX)
Miscellneous
Monthly News Letter
Free SCJP Samples
Interviews Questions For Freshers
Interviews Questions For Experinced
Most Visited Links
Most Freaky Links
Advertisements

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/geekmantra.com/contact/connect.php on line 2

Hibernate 3 (Object Relational Mapping API)

• Applications can use the JDBC API to access data residing in a relational database management system
(RDBMS).
• However, even within an RDBMS environment, the actual syntax and format of the SQL statements
may vary depending on the particular database product.
• There is even greater variation with different types of persistent storage (flat files, xml, LDAP).
• Components such as bean-managed entity beans, session beans, servlets, and other objects like helpers
for JSP pages need to retrieve and store information from persistent stores.
• But including the connectivity and data access code within these components introduces a tight coupling
between the components and the data source implementation.

So the requirement is
• Components need to be transparent to the actual persistent store or data source implementation to
provide easy migration to different vendor products, different storage types, and different data source
types.

Approach

• Use a Data Access Object (DAO) to abstract and encapsulate all access to the data source. The DAO
manages the connection with the data source to obtain and store data.

Understanding DAO


Hibernate Intro

Hibernate Intro

• Now this is clear that we will be having multiple DAO and TO classes.
• One more thing if we are designing to support multiple databases then these DAO must be created for
each type of database separately, rather then putting if-else inside the code.
• So, here it is a clear need of factory method and abstract factory pattern.

So let’s start–
All databases specific DAO must implement same set of methods, to ensure this better to create interfaces
that will be implemented by DAO classes.

Hibernate Intro

Same thing we will do for customer, order, account, department etc. We will provide a db specific factory class
that will provide underlying DAOs.

Hibernate Intro

All database specific DAO factories must provide their specific DAO, so they all must have same set of
methods. Again need of some abstraction. We can use either interface or abstract class.

Hibernate Intro

Hibernate Intro

So, now it should be very much clear that, what DAO pattern is and when it is used through factory method and
abstract factory pattern it helps us to support multiple databases comfortably.

Object Relational Mapping

• A DAO can use an ORM framework for persistence tasks.
• ORM framework is written to reduce the efforts done by DAO classes, like database connectivity,
   constructing sql queries etc.
• ORM also improve the performance of DAO layer by providing caching, query optimization etc.
• Even ORM take care of database independency that’s why we do not require using factory method and
   abstract factory pattern at DAO layer for achieving the same.
• DAO was 2 step approach, ORM will be introduced as a 3rd step, see the figure below.
• ORM will provide n number of features that we will see shortly.

Hibernate ORM

There are other options also TopLink, iBatis, JDO, EJB-Entity Beans etc but hibernate is much more powerful
and simple to use.

Using Hibernate 4 step process

We want to persist a record that keep the information about a employee through Hibernate.

Step 1 – Entity class
need to be persisted --> Employee.java ~= EmployeeTO.java

Step 2 –
Hibernate requires a mapping file (employee.hbm.xml), that keep the information like table belongs to
this employee class, column mapping and association of this entity with other existing entities.

Step 3 – framework configuration file (hibernate.cfg.xml) keeping the information about the database
connection properties, mapping files locations of entities created in step 2 etc.

Step 4 – client program where we will take Hibernate’s SessionFactory and Session then will call save(),
update() etc. method to perform persistency.

Hibernate Example

Hibernate Example

Hibernate Example

Hibernate Example