Hibernate Technology For An Efficient Business Application Extension
Hibernate Technology For An Efficient Business Application Extension
6, June 2011
Journal of Global Research in Computer Science
REVIEW ARICLE
Available Online at www.jgrcs.info
Abstract :This paper discusses hibernate technology as a novel and efficient means to access huge databases and also focuses on how to
implement persistent features in object-oriented system through it . It discusses currently available hibernate mapping framework in detail.
Hibernate provides support for collections, object relations, as well as complex and composite types. In addition to persisting objects,
hibernate also provides a rich query language to retrieve objects from the database, along with an efficient caching layer and Java
Management Extensions (JMX) support. Hibernate is a powerful, high-performance, feature-rich and very popular ORM solution for Java.
Hibernate facilitates development of persistent objects based on the common Java object model to mirror the underlying database structure.
This approach progresses the business performance to some extent, advances development efficiency exceedingly and obtains preferable
economical efficiency and practicability. In addition to, it compares and analyzes the database access efficiency resulted from two
mechanisms based on Hibernate and JDBC. This paper offers insight into hibernate technology its implementation and usage.
and Query BY Example (QBE) -using Criteria API and the class.
Native SQL queries. b) Column: The column used to store the primary key
Full support for relational operations- HQL permits value.
representing SQL queries in the form of objects. Hibernate c) Type: The Java data type is used.
Query Language uses Classes and properties instead of d) unsaved-value: This is the value used to determine if a
tables and columns. class has been made persistent. If the value of the id
Return result as Object- The HQL queries return the query attribute is null, then it means that this object has not
result(s) in the form of object(s), which is easy to use. This been persisted.
eliminates the need of creating the object and populates the 4. <generator> element: The <generator> method is used
data from result set. to generate the primary key for the new record. Here is some
Polymorphic Queries- HQL fully supports polymorphic of the frequently used generators.
queries. Polymorphic queries provide query results along a) Increment - This is used to generate primary keys of
with all the child objects if any. type long, short or int that are unique only. It should not
Easy to Learn- Hibernate Queries are easy to learn and it be used in the clustered deployment environment.
can be easily implemented in the applications. b) Sequence - Hibernate can also use the sequences to
Support for Advance features- HQL contains many generate the primary key. It can be used with DB2,
advanced features such as pagination, fetch and join with postgreSQL , Oracle, SAPDB databases.
dynamic profiling, Inner/outer/full joins and Cartesian c) Assigned - Assigned method is used when application
products. It also supports Projection, Aggregation (max, code generates the primary key.
avg) and grouping, Ordering, Sub queries and SQL function 5. <property> element: The property elements define
calls. standard Java attributes and their mapping into database
Database independent- Queries written in HQL are schema. The property element supports the column child
database independent. element to specify additional properties, such as the index
name on a column or a specific column type.
UNDERSTANDING AND IMPLEMENTATION OF
HIBERNATE MAPPING Configuring Hibernate
<?xml version="1.0"?> Hibernate can be configured by creating a property file
<!DOCTYPE hibernate-mapping SYSTEM named hibernate properties in the src directory and adding
“Hibernate_Mapping.dtd”> its path to the application's classpath. This file consists of the
<hibernate-mapping> properties used by Hibernate to connect to database[11],
<class name=”sample” table=”COMMUNICATE”> generate schema, and obtain other database-specific
<idname=”id”type=”long”column=”ID”><generator information. To reflect changes in the underlying database
class=”assigned”/> </id> into the whole application, only values of the properties in
<property name=”firstName”> <column this file need to be modified. Model 1 shows a simple
name=”FIRSTNAME”/> </property> example. Most of these properties are self-explanatory. To
<property name=”lastName”> <column set up MySQL Database in the configuration file i.e.
name=”LASTNAME”/> </property> hibernate.cfg.xml.Considering database running on the
</class> localhost. So, create the database ("hibernate tutorial") on
</hibernate-mapping> the MySQL server running on local host.
Hibernate mapping documents are simple xml documents
[10]. Model 1: A simple example of hibernate properties file:
Here are some important elements of the mapping file: hibernate.connection.driver_class =
1. <hibernate-mapping> element: The first or root element COM.ibm.db2.jdbc.app.DB2Driver
of hibernate mapping document is <hibernate-mapping> hibernate.connection.url = jdbc: db2: inventory
element between the <hibernate-mapping> tag class hibernate.connection.username = db2admin
element(s) are present [3]. hibernate.connection.password = Taman
2. <class> element: The <class> element maps the class hibernate.dialect = cirrus.hibernate.sql.DB2Dialect
object with corresponding entity in the database. It also tells
what table in the database has to access and what column in Hibernate also can be configured by using a simple XML
that table it should use. Within one <hibernate- mapping> file named hibernate.cfg.xml, which exists inside the src
element, several <class> mappings are possible. directory. This file's structure is very similar to hibernate
3. <id> element: The <id> element is unique identifier to properties and has the same functionality. This shown as
identify and object. In fact <id> element map with the example
primary key of the table in our code <id name="id" in the following model.
type="long" column="ID" > primary key maps to the ID
field of the table COMMUNICATE. The attributes of the id Model 2: Example of a simple hibernate.cfg.xml
element are: <? xml version="1.0"?>
a) Name: The property name used by the persistent <!DOCTYPE hibernate-mapping SYSTEM
“Hibernate_Mapping.dtd”>
© JGRCS 2010, All Rights Reserved 120
B.Vasavi et al, Journal of Global Research in Computer Science, Volume 2 Issue (6), June 2011,
A simple general overview of the logic looks like the Session session2 =
following, sessionFactory.openSession(myAnotherInterceptor);
- Insert/Update the records in the Database User u1, u2 = null;
- During Insert/Update, maintain the log information in a // assume u1 and u2 objects are associated with session
file as we can see, the maintenance of this logging 'session2'.
information should happen whenever an insert/update goes
to the Database. Such a logger interceptor can be easily From the above code, we can infer that a session-scoped
plugged into the application with minimal code change interceptor can be set by calling the method
because of the flexible design of hibernate. SessionFactory.openSession(Interceptor). In the above code,
we have two different session objects 'session1' and
Types of Interceptors 'session2' being configured with Interceptors MyInterceptor
Based on their scope, Interceptors in hibernate can fall under and MyAnotherInterceptor respectively. So, e1 and e2
two categories. They are, objects will be affected by MyInterceptor, whereas u1 and
• Application-scoped Interceptors u2 objects will be affected by MyAnotherInterceptor.
• Session-scoped Interceptors
IMPLEMENTATION FOR TESTING HIBERNATE
Application-scoped Interceptor APPLICATION
An application can contain one or more database sessions Now we are ready to write a program to insert the data into
represented by the Session interface. If an application is database. We should first understand about the hibernates
configured to use Global Interceptors, then it will affect the Session. Hibernate Session is the main runtime interface
persistent objects in all the sessions. The following code between a Java application and hibernate [7]. First we are
configures a global interceptor. required to get the hibernate Session. SessionFactory allows
SessionFactory sessionFactory = application to create the hibernate Session by reading the
configuration.buildSessionFactory(); configuration from hibernate.cfg.xml file. Then the save
Session session1 = sessionFactory.openSession(); method on session object is used to save the contact
Employee e1, e2 = null; information to the database.
// Assume e1 and e2 objects are associated with session1. session.save(communicate)
Session session2 = sessionFactory.openSession(); Here is the code of First Example1.java
User u1, u2 = null package roseindia.tutorial.hibernate;
//Assume u1 and u2 objects are associated with session1. import org.hibernate.Session;
import org.hibernate.SessionFactory;
A global-scoped interceptor can be set to an application by import org.hibernate.cfg.Configuration;
calling the Configuration.setInterceptor(Interceptor) method. public class FirstExample {
In the above code, we have two different session objects public static void main(String[] args) {
'session1' and 'session2'. Let us assume that e1 and e2 Session session = null;
Employee objects are associated with session 'session1' and try{
u1 and u2 are the user objects associated with session // This step will read hibernate.cfg.xml and prepare hibernate
'session2'. The applied application-scoped interceptor would for use
have affected all the objects (e1, e2, u1 and u2), even though SessionFactory sessionFactory = new
they are in different sessions. Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Session-scoped Interceptor //Create new instance of Communicate and set
A session-scoped interceptor will affect all the persistent values in it by reading them from form object
objects that are associated with that particular session only. System.out.println("Inserting Record");
The following code shows how to configure a session- Communicate contact = new Communicate();
scoped interceptor. contact.setId(3);
Configuration configuration = new Configuration(); contact.setFirstName("jasmine");
SessionFactory sessionFactory = contact.setLastName("Rao");
configuration.buildSessionFactory(); contact.setEmail("jasmine_74@yahoo.com");
MyInterceptor myInterceptor = new MyInterceptor(); session.save(contact);
Session session1 = System.out.println("Done");
sessionFactory.openSession(myInterceptor);
Employee e1, e2 = null; Retrieving Persistent Classes
// Assume e1 and e2 objects are associated with session If you know the primary key value of the object that you
'session1'. want to retrieve, you can load it with the Session.load()
MyAnotherInterceptor myAnotherInterceptor = new method[6]. This method is overloaded to offer support for
MyAnotherInterceptor (); standard classes and BMP entity beans. To retrieve a
persistent class without knowing its primary key value, you
performance, open source persistence framework Technology and Management, Hyderabad [HITAM], A.P,
comparable to many of its open source and commercial India. She has presented many papers to her credit at National
counterparts. Developers utilizing Hibernate can greatly and National conferences. She has presided over as judge to
reduce the amount of time and effort needed to code, test, many Paper Presentations and Technical Quizzes. She has
and deploy applications. Hibernate is a powerful, high- authored 4 research papers and are published in reputed and
performance, feature-rich and very popular ORM solution indexed International Computer Science Journals. She has
for Java along with mapping objects to a database. As guided 20 Students of Master degree in Computer Science and
discussed above hibernate also provides advanced data query Engineering in their major projects. She is bestowed with the
and retrieval services through HQL, efficient caching, and Editorial Member on five International Journals Boards and is
other optimization techniques with useful built-in utilities for nominated as Reviewer to three International Journals. Her
coding and schema generation. This automats the generation area of research includes TIBCO; Cloud computing, Network
of a persistent layer to a large extent and hence, helps in Security, Image Processing, Data Mining, Web Technologies
relieving the developer up to 95% of common persistence and Emerging Technologies. She can be reached at:
related coding. vasavi.bande@yahoo.co.in
REFERENCES:
[1] Beginning Hibernate: from novice to professional,
jefflinwood .
[2] http://www.devarticles.com.
[3] http://www.mindfiresolutions.com. 2. Y.V.Sreevani Graduated in AM.I.E.T.E. from
I.E.T.E, New Delhi, India, in 1997 and M.Tech in Computer
[4] Professional Hibernate (programmer to
science from Osmania University, Hyderabad, A.P., India in
programmer),Ericpugh . 2003. She has published 3 International papers. She is presently
[5] http://www.apress.com. working as Associate Professor in Department of Computer
[6] Java Persistence with Hibernate Second Edition of Science and Engineering, Hyderabad Institute of Technology and
Hibernate in Action Christian Bauer and Gavin King. Management, Hyderabad [HITAM], A.P, India. Her area of
[7] http://www.yangdaoqi.info. research includes Network Security, Data Mining, Web Mining
[8] Hibernate in Action, Christian Bauer and Gavin King. Technologies and Emerging Technologies. She can be reached
[9] Spring Persistence with Hibernate, AhmadSeddighi. at: s_vanikumar@yahoo.co.in
[10] Web development with java:using Hibernate,jsps and
servlets,TimDowney.
[11] JBoss as 5 developments, Francescomarchoni.
Authors:
3. G. Sindhu Priya is pursuing B.Tech in
Computer Science and Engineering, Hyderabad Institute of
Technology and Management, Hyderabad [HITAM], A.P,
India. She has participated in number of paper presentations
and technical workshops. She is a noted and active member of
1. Vasavi Bande is M.Tech in Computer Science from the Sahaya Society, a organization to educate the less
Jawaharlal Nehru Technological University, A.P., India. She privileged school students i.e. www.sahayasociety.org. She
has vast experience in Computer Science and Engineering area has organized number of events which include Cultural,
pertaining to academics and industry related real time projects. Symposiums and blood donation camps. She can be reached:
She is presently working as Associate Professor in Department sindhupriya78@gmail.com
of Computer Science and Engineering, Hyderabad Institute of