Advertisment

Object Persistence with Hibernate

author-image
PCQ Bureau
New Update

Hibernate, a component of the JBoss middleware system suite
of products allows you to develop persistent classes following object-oriented
paradigm -involving inheritance, association, composition, polymorphism and
collections. Hibernate makes it seem as if your database contains POJO (Plain
Old Java Objects), without having to worry about how to get them out of and
persist them back. Positioned as a layer between your application and your
database, Hibernate takes care of loading and saving the objects. It's a boon
to Java developers whose main purpose is to focus on business logic without
worrying about the underlying database.

Advertisment

Hibernate is intended to relieve the developer from common
data persistence related programming tasks, unlike coding manually with SQL and
the JDBC API. It offloads you from manual JDBC result set handling and object
conversion, and keeps your application portable to all relational databases.
Hibernate takes care of the inevitable mismatch between your object-oriented
code and the relational database requirements. It helps you to express queries
in its own portable SQL extension (HQL), as well as in native SQL, thus,
offering multiple query options, further you can fire polymorphic queries. With
Hibernate, you don't even need tables as it can map entities and particular
properties to SQL expressions.

Direct
Hit!
Applies
to:
Java developers
USP:
Leverage from lightweight OR (Object Relational) mapping
Links:http://hibernate.org
Google keywords:
Hibernate, O/R Mapping

In this series of two articles, we'll first give an
overview of Hibernate O/R mapping framework and then walk you through the code.

Advertisment

Why use Hibernate?

Object persistence is not new. There are many commercial offerings for the
popular EJB specification but they often end up being complex, and require
special treatment by the implementing code. Hibernate is a lightweight solution
which offers transparent persistence without forcing the developer to implement
any special interfaces or extend from the framework API.  

Hibernate is licensed under the GNU public license. It
supports mappings for collections and dependent objects to the underlying
database tables. There's no extra code generation. Hibernate implements
persistence API and query language defined by EJB 3.0 specifications. Hibernate
ships with two open source connection pools. With Hibernate, you can save a lot
of redundant data-access code, you don't have to write complex SQL queries,
and it just requires a few lines to make a connection to a database, select some
results, and print them out to the screen or some log file. In a nutshell, you
don't need to write that extra code to persist your business logic leading to
automation and writing less code.

Streamlining App development 

For any enterprise application development a lot of time is utilized for
designing and maintaining a persistence layer. If changes are made to the
underlying database schema, it can be expensive to propagate those changes to
the rest of the application, both in terms of time and efforts. This is exactly
where Hibernate steps in to fill this gap, providing an easy-to-use and powerful
object-relational persistence framework. Your business logic is free from any
data access code. Hibernate will help us to store the data in permanent storage
without any hassles about choosing the kind of storage, installation, or
configuration. Hibernate allows us to store any kind of objects. Powered by
runtime reflection to determine the persistent properties of a class, the
overhead is much less than traditional JDBC calls. Hibernate can make multiple
optimizations when interacting with the database, including object caching,
efficient outer join fetching and executing SQL statements only when needed. It
is difficult to achieve this level of sophistication with hard-coded JDBC.
Further on scalability standpoint, unlike EJB, Hibernate does not automatically
do instance-pooling and, there's no sharing of objects between concurrent
threads. Developers utilizing Hibernate can greatly reduce the amount of time
and efforts needed to code, test, and deploy applications.

Advertisment
Hibernate sits below the middle tier in the hierarchy

What you can do

 The Hibernate project is available in a number of different packages
ranging from Hibernate core, annotations, entity manager (to interact with EJB
3.0 EntityManager API) to NHibernate, which helps in persisting plain .NET
objects.  Hibernate doesn't expect you to implement any special
interfaces or extent form any predefined class. All you need to do is create an
XML mapping document directing Hibernate the classes that you want to store in a
database, and how they relate to the tables and columns in the underlying
database.

Then you can ask it to fetch data as objects, or store
objects as data. Hibernate requires metadata that governs the transformation of
data from one representation to the other and vice versa. Most of the time in
Hibernate 2.x, mapping metadata is declared in XML documents. Serialized
persistent objects may be selectively re-associated with Hibernate, and their
state synchronized with the database. Modifications to objects associated with a
transaction are automatically detected and propagated to the database. Hibernate
supports bidirectional association. Further, Hibernate supports Java arrays,
collections framework and user-defined types. As a developer you don't need to
worry about constraint transparency such as foreign key violations; primary keys
may span multiple properties of a class, they all are taken care by the
framework itself. With Hibernate, query results may be accessed using a
scrollable result set. Hibernate supports sub queries, joins, stored procedures,
projection, aggregation and grouping.

Advertisment

Conclusion

Established in 2001, Hibernate has evolved as a mature and robust
technology. It offers the developers clear, concise and powerful tools for
working with relational databases. Hibernate can work with a servlet container (eg
Tomcat), an app server such as JBoss or even in standalone applications. As a
developer, you can look at

Hibernate as a way to persist your business logic outside
the container. Hibernate quickly adapts to your development process, no matter
you are starting with a design from scratch or already working with an existing
database. Moreover, it supports multiple application architectures. Stay tuned
for the next article in this series where we'll get our hands dirty with some
simple Hibernate code.

Kunal Jaggi

Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: