How Hibernate Works
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.
Hibernate Architecture
There are two common (recommended) architectures can be seen including Hibernate as
a persistence layer.
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.
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.
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.
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 {
<?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
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();
}
}
}
<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>