Some reflections on using Hibernate

In this post I will go over some of the main advantages and disadvantages of using Hibernate. You may be thinking about using it or maybe you’re trying to persuade your manager to let you use it. Or maybe you’re looking for a way to impress a girl on a date by showing off your Hibernate expertise! Probably not the last one actually……


Advantages of using Hibernate

Better than using raw JDBC

Hibernate provides an easy way of mapping your objects to your tables, so you don’t have to write JDBC calls every time you need something from the database. Instead you get to deal with objects and write nice queries using HQL (Hibernate Query Language) where you write queries using objects instead of tables.

Creates a bridge between the objects in your application and the database

When you’re programming in Java you are programming in an object orientated manner where every component is viewed as an object. However when dealing with databases you are dealing with a more mathematical model, hence mapping between the two is tricky. This issue is more commonly known as the object-relational impedance mismatch.

Hibernate resolves this by allowing the user to think of any persistence related tasks using entities or objects instead of having to think about rows and tables.

Layer of abstraction

Having an extra layer between your database and your application means that if you ever need to change anything database related such as a table name or column you only have to do it one place, that would be much trickier if you had just been using JDBC.

The Standard in ORM

Hibernate is considered to be the accepted standard in object relational mapping and is also an implementation of the Java Persistence API which is a key part of the JEE platform.

Established framework

Hibernate is an open source framework has been around for many years, there is lots of development activity and has been used on many different projects. Hence this reduces the risk that it will become obsolete any time soon and that learning it would have been a waste of time.

Disadvantages of using Hibernate

Large object graph

If you’re not careful you can end up loading a large number of objects from the database which then reside in your memory.

One approach to resolving this is by using lazy loading, which has a few of its own caveats.

Can be a pain with complex legacy databases

Hibernate works best and easiest with a brand new database, attempting to use it on an existing legacy database can be quite a difficult task. You end up having to use composite primary keys and large numbers of native SQL queries.

Steep learning curve

It is quite easy to do the basics in hibernate but to really use hibernate well you need a good knowledge of ORM concepts, the way mappings and the session works and also more advanced concepts such as caching, lazy loading and fetching strategies.

Performance optimisation

Trying to optimise performance at the database level is a bit trickier with Hibernate due to the fact the SQL that is executed is actually generated by Hibernate, hence you are not able to just go in and adjust the SQL accordingly.

Issue when using with a Web/MVC application

Lazy loading is the big gotcha in MVC applications that use Hibernate for their persistence. All you want to do is load a collection of objects and display them on the page, but for some reason you keep getting a LazyInitializationException. The root cause of this is that the logic of the application has completed and session has been closed before the view was completely rendered.

Conclusion

Hibernate is an excellent framework to use for ORM based persistence, but as with everything in life there is a cost involved. Also there are alternatives such as iBatis and Spring’s JDBC framework that are worth looking at.

VN:F [1.1.5_471]
Rating: 9.3/10 (3 votes cast)
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Slashdot
  • StumbleUpon
  • TwitThis

No related posts.