0% found this document useful (0 votes)
56 views6 pages

How Hibernate Works

Hibernate is an object-relational mapping tool that allows developers to interact with a database using object-oriented programming. It uses XML configuration files to map classes to database tables and handles the SQL needed for data retrieval and storage. At runtime, Hibernate uses reflection to handle object-relational mapping without the need for code generation. It provides a session interface for managing persistence across a single thread and handles transactions through its architecture, which includes connection pooling and management.

Uploaded by

Laxman Kasarla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

How Hibernate Works

Hibernate is an object-relational mapping tool that allows developers to interact with a database using object-oriented programming. It uses XML configuration files to map classes to database tables and handles the SQL needed for data retrieval and storage. At runtime, Hibernate uses reflection to handle object-relational mapping without the need for code generation. It provides a session interface for managing persistence across a single thread and handles transactions through its architecture, which includes connection pooling and management.

Uploaded by

Laxman Kasarla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

How Hibernate Works?

Hibernate is driven by XML configuration files to configure data connectivity and map
classes to database tables with which it needs to interact. These XML files contain database
connection specifics, connection pooling details, transaction factory settings, as well as
references to other XML files that describe tables in the database.

When developer writes code to call API, the called API executes necessary SQL at runtime.
Rather than use of byte-code processing or code generation, Hibernate uses runtime
reflection to resolve the persistent properties of a class. The persisted objects are defined
in a mapping
document, which describes the persistent fields and associations, as well as subclasses or
proxies of the persistent object. The mapping documents are compiled at the time of
application startup and provide the framework
with necessary information for a class.

At the compile-time of mapping documents, a SessionFactory is also created that


provides the mechanism for managing persistent classes, and the Session interface. The
Session class provides the interface between the persistent stored data and the
application. The Session interface wraps a JDBC connection, which can be user-managed
or controlled by Hibernate. This interface is intended only used by a single
threadapplication. That means after completing the
session, application is closed and discarded.

Hibernate Architecture

Unlike other technologies, Hibernate provides persistence as a service, rather than as a


framework. It integrates flawlessly with various application architectures.

There are two common (recommended) architectures can be seen including Hibernate as
a persistence layer.

The following diagram describes the Web (twotiered) Architecture of


Hibernate The above diagram shows that Hibernate
is using XML mapping to configure data connectivity to database tables, and map
classes for providing persistence services (and persistent objects) to the application.

To use Hibernate, it is required to create Java classes that represent the table in the
database and then map the instance variable in the class with the columns in the database.
Once the mapping is complete, various operations like select, insert, update and delete
the records can be performed by the Hibernate on the table of database. Hibernate
automatically creates the query to perform these operations. It may also be used to persist
JavaBeans used by servlets/JSPs in Model View Controller (MVC) architecture.

Now, the following diagram describes the Enterprise (three-tiered) Architecture of


Hibernate: The above diagram shows that Hibernate is using the database and
configuration data to provide persistence services (and persistent objects) to the
application.

To use Hibernate, it is required to create Java classes that represents the table in the
database and then map the instance variable in the class with the columns in the database.
Then Hibernate can be used to perform operations on the database like select, insert,
update and delete the records in the table. Hibernate automatically creates the query to
perform these operations.

Hibernate architecture has three main components:

 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 is very good tool as far as object relational mapping is concern, but in terms of
connection management and transaction management, it is lacking in performance and
capabilities. So usually hibernate is being used with other connection management and
transaction management tools. For example apache DBCP is used for connection pooling
with the Hibernate.

Hibernate provides a lot of flexibility in use. It is called "Lite" architecture when we only
uses the object relational mapping component. While in "Full Cream" architecture all the
three component Object Relational mapping, Connection Management and Transaction
Management) are used.

/**
*
*/
package roseindia.tutorial.hibernate;

/**
* @author Administrator
*
*/
public class Contact {

private String firstName;


private String lastName;
private String email;
private int id;

public String getEmail() {


return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Contact" table="CONTACT">
<id name="id" type="int" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>
/**
*
*/
package roseindia.tutorial.hibernate;

import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Administrator
*
*/
public class HibernateExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
org.hibernate.Transaction tr = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from
form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Nisha");
contact.setLastName("Gupta");
contact.setEmail("[email protected]");
session.save(contact);
tr.commit();
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}
finally{
// Actual contact insertion will happen at this step
//session.flush();
session.close();

}
}
}

<?xml version='1.0' encoding='utf-8'?>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-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://192.168.10.52/hibernatetutorial</property
>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

You might also like