Sunday, 12 March 2017

Disadvantages of Hibernate


Hibernate ORM (Hibernate in short) is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database.
There are basically one to one mapping, one to many mapping and many to many mapping that eases the development. But there are many disadvantages of hibernate.

In hibernate, everything is returned as an object, and you have to fetch the entire row - NO. We can fetch single columns or some selected columns. This data can be either populated in the entity bean itself, or can be returned as a map with the key as column name. If fetching single column, no need of entity or map.

It takes more time that JdBC? NO. If your application is huge, and you have a huge database, and you make a lot of queries- jdbc has overheads, of closing and opening connections, managing transactions. Hibernate handled everything. It also caches data.

Join is not possible. NO. If you create the entity properly as per the joins required, it pretty much handles all the scenarios. You can create multiple entities for same table, so 1 entity can be created without the join, for the queries which don't require joins, and other entity can be used for queries which need a join.

If you you have a small set of tables, probably you may not notice the difference, but in huge applications hibernate is a plus. It helps cache data, thereby reducing response time, it manages the transactions and you don't need to worry much about them, and you don't have to bother about making connection and closing connection every time you write a query in your code.

Also we can not ignore the hibernate inheritance while discussing about hibernate features.

 Performance Cost

Hibernate generates lot of SQL statements in runtime based on our mapping , so it is bit slower than JDBC.

If we use JDBC, then we directly write our queries, so no extra effort is required.

Does not allow multiple inserts

Hibernate does not allow some queries which are supported by JDBC.

For example, It does not allow to insert multiple objects (persistent data) to same table using single query.

Developer has to write separate query to insert each object.

More Complex with joins

If there are lot of mappings and joins among the tables, then code becomes bit complex to understand as we need to define the mapping and join information in the XML file.

Poor performance in Batch processing:

It advisable to use pure JDBC for batch processing as Hibernate performance is not good in Batch processing.

Not good for small project.

Small project will have less number of tables , introducing entire Hibernate framework will be overhead than useful.

Learning curve

As many other tool, Hibernate also takes considerable amount of time and effort to learn. I can’t say it is 100% disadvantage but yes it’s not so easy to learn.

So because of steep learning curve, we may consider it as disadvantage.

Performance : A lot is being talked about hibernate performance. We have used Spring and hibernate with one of the biggest deployment in clinical applications and I can tell you, I have not seen any issues or so because of hibernate.

Yes we had to fix the HQLs at few places but that is a normal tuning process.

Lots of API to learn: A lot of effort is required to learn Hibernate. So, not very easy to learn hibernate easily.

Debugging: Sometimes debugging and performance tuning becomes difficult.

Slower than JDBC: Hibernate is slower than pure JDBC as it is generating lots of SQL statements in runtime.

Not suitable for Batch processing: It advisable to use pure JDBC for batch processing.

Not suitable for Small projects : For small project having few tables it is useless to work with hibernate.

Does not allow multiple inserts : Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.

Generates complex quires with many joins : For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion.

Everything is returned as object : Yes that is true and even if you would like to get an id or name, you would write the hql and get an object which has the complete data set. This is anyways not bad because while designing methods, you will have coarse grained objects which well be returned. I would not prefer to return String or int from my methods.
For more topic on hibernate visit here - Hibernate Tutorials

No comments:

Post a Comment