ORM (object-relational mapping) is used to map object-oriented programming objects to relational databases managed by Oracle, DB2, Sybase, and other relational database managers (RDBMSs). Object-relational mapping products are designed to work well with several object-programming languages like C#, C++, and Java. Database objects appear as programming language objects. Often, the interface for object-relational mapping products is the same as the interface for object-oriented databases. RDBMS cannot store objects directly; ORM as provided with Hibernate tries to help this process. This paper will discuss using Hibernate as an object-relational mapping (ORM) library with Java.
Hibernate as an ORM library with Java
Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files. Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks. (Hibernate, 2009) Hibernate allows data to be stored in permanent storage without a lot of consideration for kind of storage, installation, or configuration. Hibernate allows the storage of any kind of objects; therefore, the application does not need to know that its data will be persisted using Hibernate. The opposite can also be applied as far as retrieving already prepared objects from storage. Updating and deleting data is also available with Hibernate.
The benefits of using Hibernate as an object-relational mapping (ORM) library with Java
Using Hibernate can save a lot of development time on a project, since the framework manages the whole JDBC layer. This means that your application's data access layer will sit above Hibernate and is completely abstracted away from the underlying data model. Hibernate has a number of advantages over other similar object-relational mapping approaches (JDO, entity beans, in-house development, and so on) it's free and open source, it has achieved a good level of maturity, it's widely used, and it has a very active community forum. Hibernate is in my opinion the most popular and most complete open source object/relational mapping solution for Java environments. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time; otherwise, spent with manual data handling in SQL and JDBC. It manages the database and the mapping between the database and the objects. Hibernates goal is to relieve the developer from 95 percent of common data persistence related programming tasks. Hibernate adapts to your development process, no matter if you start with a design from scratch or work with a legacy database.
Hibernate generates SQL for you, relieves you from manual result set handling and object conversion, and keeps your application portable to all SQL databases. Hibernate allows you to store, fetch, update and delete any kind of objects. Hibernate lets you develop persistent classes following common Java idiom-including association, inheritance, polymorphism, composition, and the Java collections framework. The Hibernate Query Language, designed as a minimal object-oriented extension to SQL, provides an elegant bridge between the object and relational worlds.
There are many advantages to using Hibernate. Along the lines of productivity, Hibernate improves the productivity by eliminating the JDBC time-consuming code. It also provides good API to most common CRUD operations so that most of the JDBC code can be eliminated. From a maintenance advantage, point the number of lines of code is decreased significantly. It is easier to understand and lets you concentrate on the Business logic. Changes to the code are easy to implement. From a performance standpoint the budget and timeline for Hibernate is a better alternative to other frameworks. Hibernate also provides a cache mechanism to reduce hits to the database with caching statements. This can be very important when querying live, production databases. For example, the Health Information Database in McKesson's STAR System has a very large database that is attached to several live systems. Any impact on the database can slow the live environment down and have a negative affect on patient care. In many cases, any degradation to the system is not an option.
The drawbacks of using Hibernate as an object-relational mapping (ORM) library with Java
As with any framework, there are disadvantages that must be weighed against the advantages to ensure the proper one is chosen for the project. What is expected from a relational data persistence solution is good support for dynamic query generation. Hibernate offers two options: HQL and the criteria API. HQL is a language of its own, which you have to learn first. It is a SQL dialect with Java code mapping extensions. It is provided or assembled using unsafe string literals and the problem comes when trying to build complex statements with conditionally added constraints and joins. At some point of complexity, the building code can become unmanageable. The criteria API is better from this respect but limited in its capabilities.
Complexity of the application code with Hibernate
A common programming task is that you need a special view of your data that will contain only few columns collected or even calculated from one or more tables. The result may be displayed to a user or used for other processing purposes. For this task, Hibernate HQL offers the possibility to define the select clause, ideally giving it a special "result" bean that holds exactly the data that you need, even transformed with SQL functions such as string concatenation or numeric calculations. In many projects, this feature is rarely used. Instead, programmers work with the full entity beans, which mean that far more attributes than necessary are loaded from the database. For entity relation resolving Hibernate either uses joins (eager mode) loading all referenced entities as well or - with lazy loading enabled - additional queries are performed, one for each unique referenced object - sometimes even just to access one simple attribute. So in fact instead of one bean object per row holding say five attributes, five objects a row holding together over 50 attributes are loaded. It is obvious that this is not the perfect solution.
Hibernate does provide transparent persistence and the developer does not need to
write code explicitly to map database tables to application objects during interaction with
RDBMS. Hibernate is flexible and a powerful ORM solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this. Hibernate reduces lines of code by maintaining object-table mapping itself and returns results to applications in the form of Java objects. Hibernate, with Transparent Persistence, cache is set to application work space. Relational tuples are moved to this cache as a result of the query. It improves performance if client application reads same data many times for same write. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code. Hibernate provides a powerful query language Hibernate Query Language (independent from the type of database) that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application. (Hibernate, 2009)
Hibernate provides some add-on functionalities that are not mentioned in the JPA specification. Hibernate doesn't replace JDBC. Hibernate is sitting on top of JDBC to connect to the database. Internally Hibernate is using these JDBC calls to connect to the database systems. Hibernate is neither forcing its developers to use some specific interfaces for the classes to be persisted in an application nor it requires the program to follow some standard design to achieve its goals. For this reason, Hibernate can well integrate with all kinds any of J2SE/J2EE application and with any kind of frameworks (like Spring, Struts etc).
Another drawback to consider is that Hibernate has several levels and kinds of cache. There is a first level cache of Hibernate entities stored in and scoped specifically to a particular open Session. In Hibernate, entity objects are never shared between sessions. There is also a second level cache that is shared across sessions. The second level cache is divided into regions of four types: entity, collection, query, and timestamp. Entity and collection regions cache the data from entities and relationships (but not the entities themselves). The query and timestamp caches are related. The query cache is used to store the result set of a query made against the database. The key of the query cache contains the query string itself and any bind parameters passed with the query. The value consists not of the entity field values but just the primary key identifiers for all of the entities returned by the query. When a query is made that hits the query cache, that set of entity identifiers can be retrieved and then resolved through the first or second level caches instead of retrieving those entities from the database.
The timestamp cache keeps track of the last update timestamp for each table (this timestamp is updated for any table modification). If query caching is on, there is exactly one timestamp cache and it is utilized by all query cache instances. Any time the query cache is checked for a query, the timestamp cache is checked for all tables in the query. If the timestamp of the last update on a table is greater than the time the query results were cached, then the entry is removed and the lookup is a miss. The improvement to performance may not be a trade for a lookup miss.
Conclusion
Hibernate lets you develop persistent classes following natural Object-oriented idioms including inheritance, polymorphism, association, composition, and the Java collections framework. Hibernate requires no interfaces or base classes for persistent classes and enables any class or data structure to be persistent. Furthermore, Hibernate enables faster build procedures since it does not introduce build-time source or byte code generation or processing. Hibernate supports lazy initialization, many fetching strategies, and optimistic locking with automatic versioning and time stamping. Hibernate requires no special database tables or fields and generates much of the SQL at system initialization time instead of runtime. Hibernate consistently offers superior performance over straight JDBC coding. Hibernate is well known for its excellent stability and quality, proven by the acceptance and use by tens of thousands of Java developers.