[ACCEPTED]-Hibernate or JDBC-jdbc

Accepted answer
Score: 188

Good question with no single simple answer.

I 93 used to be a big fan of Hibernate after 92 using it in multiple projects over multiple 91 years. I used to believe that any project 90 should default to hibernate.

Today I am not 89 so sure.

Hibernate (and JPA) is great for 88 some things, especially early in the development 87 cycle. It is much faster to get to something 86 working with Hibernate than it is with JDBC. You 85 get a lot of features for free - caching, optimistic 84 locking and so on.

On the other hand it has 83 some hidden costs. Hibernate is deceivingly simple when you start. Follow some tutorial, put 82 some annotations on your class - and you've 81 got yourself persistence. But it's not simple 80 and to be able to write good code in it 79 requires good understanding of both it's 78 internal workings and database design. If 77 you are just starting you may not be aware 76 of some issues that may bite you later on, so 75 here is an incomplete list.


The runtime performance 74 is good enough, I have yet to see a situation 73 where hibernate was the reason for poor 72 performance in production. The problem is the startup 71 performance and how it affects your unit 70 tests time and development performance. When 69 hibernate loads it analyzes all entities 68 and does a lot of pre-caching - it can take 67 about 5-10-15 seconds for a not very big 66 application. So your 1 second unit test 65 is going to take 11 secods now. Not fun.

Database Independency

It 64 is very cool as long as you don't need to 63 do some fine tuning on the database.

In-memory Session

For 62 every transaction Hibernate will store an 61 object in memory for every database row 60 it "touches". It's a nice optimization when 59 you are doing some simple data entry. If 58 you need to process lots of objects for 57 some reason though, it can seriously affect 56 performance, unless you explicitly and carefully 55 clean up the in-memory session on your own.


Cascades 54 allow you to simplify working with object 53 graphs. For example if you have a root object 52 and some children and you save root object, you 51 can configure hibernate to save children 50 as well. The problem starts when your object 49 graph grow complex. Unless you are extremely 48 careful and have a good understanding of 47 what goes on internally, it's easy to mess 46 this up. And when you do it is very hard 45 to debug those problems.

Lazy Loading

Lazy Loading means 44 that every time you load an object, hibernate 43 will not load all it's related objects but 42 instead will provide place holders which 41 will be resolved as soon as you try to access 40 them. Great optimization right? It is, except 39 you need to be aware of this behaviour otherwise 38 you will get cryptic errors. Google "LazyInitializationException" for 37 an example. And be careful with performance. Depending 36 on the order of how you load your objects 35 and your object graph you may hit "n+1 selects 34 problem". Google it for more information.

Schema Upgrades

Hibernate 33 allows easy schema changes by just refactoring 32 java code and restarting. It's great when 31 you start. But then you release version 30 one. And unless you want to lose your customers 29 you need to provide them schema upgrade 28 scripts. Which means no more simple refactoring 27 as all schema changes must be done in SQL.

Views and Stored Procedures

Hibernate 26 requires exclusive write access to the data 25 it works with. Which means you can't really 24 use views, stored procedures and triggers 23 as those can cause changes to data with 22 hibernate not aware of them. You can have 21 some external processes writing data to 20 the database in a separate transactions. But 19 if you do, your cache will have invalid 18 data. Which is one more thing to care about.

Single Threaded Sessions

Hibernate 17 sessions are single threaded. Any object 16 loaded through a session can only be accessed 15 (including reading) from the same thread. This 14 is acceptable for server side applications 13 but might complicate things unnecessary 12 if you are doing GUI based application.

I 11 guess my point is that there are no free meals.

Hibernate is a good 10 tool, but it's a complex tool, and it requires 9 time to understand it properly. If you or 8 your team members don't have such knowledge 7 it might be simpler and faster to go with 6 pure JDBC (or Spring JDBC) for a single 5 application. On the other hand if you are 4 willing to invest time into learning it 3 (including learning by doing and debugging) than 2 in the future you will be able to understand 1 the tradeoffs better.

Score: 20

Hibernate can be good but it and other JPA 12 ORMs tend to dictate your database structure 11 to a degree. For example, composite primary 10 keys can be done in Hibernate/JPA but they're 9 a little awkward. There are other examples.

If 8 you're comfortable with SQL I would strongly 7 suggest you take a look at Ibatis. It can do 90%+ of 6 what Hibernate can but is far simpler in 5 implementation.

I can't think of a single 4 reason why I'd ever choose straight JDBC 3 (or even Spring JDBC) over Ibatis. Hibernate 2 is a more complex choice.

Take a look at 1 the Spring and Ibatis Tutorial.

Score: 13

No doubt Hibernate has its complexity.

But 35 what I really like about the Hibernate approach 34 (some others too) is the conceptual model 33 you can get in Java is better. Although 32 I don't think of OO as a panacea, and I 31 don't look for theoritical purity of the 30 design, I found so many times that OO does 29 in fact simplify my code. As you asked specifically 28 for details, here are some examples :

  • the 27 added complexity is not in the model and entities, but in your framework for 26 manipulating all entities for example. For 25 maintainers, the hard part is not a few 24 framework classes but your model, so Hibernate 23 allows you to keep the hard part (the model) at 22 its cleanest.

  • if a field (like an id, or 21 audit fields, etc) is used in all your entities, then 20 you can create a superclass with it. Therefore :

    • you write less code, but more importantly ...
    • there are less concepts in your model (the unique concept is unique in the code)
    • for free, you can write code more generic, that provided with an entity (unknown, no type-switching or cast), allows you to access the id.
  • Hibernate 19 has also many features to deal with other 18 model caracteristics you might need (now 17 or later, add them only as needed). Take 16 it as an extensibility quality for your design.

    • You might replace inheritance (subclassing) by composition (several entities having a same member, that contains a few related fields that happen to be needed in several entities).
    • There can be inheritance between a few of your entities. It often happens that you have two tables that have pretty much the same structure (but you don't want to store all data in one table, because you would loose referential integrity to a different parent table).
  • With reuse 15 between your entities (but only appropriate inheritance, and composition), there 14 is usually some additional advantages to 13 come. Examples :

    • there is often some way to read the data of the entities that is similar but different. Suppose I read the "title" field for three entities, but for some I replace the result with a differing default value if it is null. It is easy to have a signature "getActualTitle" (in a superclass or an interface), and implement the default value handling in the three implementations. That means the code out of my entities just deals with the concept of an "actual title" (I made this functional concept explicit), and the method inheritance takes care of executing the correct code (no more switch or if, no code duplication).
    • ...
  • Over time, the requirements 12 evolve. There will be a point where your 11 database structure has problems. With JDBC 10 alone, any change to the database must impact 9 the code (ie. double cost). With Hibernate, many 8 changes can be absorbed by changing only 7 the mapping, not the code. The same happens 6 the other way around : Hibernate lets you 5 change your code (between versions for example) without 4 altering your database (changing the mapping, although 3 it is not always sufficient). To summarize, Hibernate lets your evolve your database and your code independtly.

For 2 all these reasons, I would choose Hibernate 1 :-)

Score: 5

I think either is a fine choice, but personally 13 I would use hibernate. I don't think hibernate 12 is overkill for a project of that size.

Where 11 Hibernate really shines for me is dealing 10 with relationships between entities/tables. Doing 9 JDBC by hand can take a lot of code if you 8 deal with modifying parent and children 7 (grandchildren, siblings, etc) at the same 6 time. Hibernate can make this a breeze 5 (often a single save of the parent entity 4 is enough).

There are certainly complexities 3 when dealing with Hibernate though, such 2 as understanding how the Session flushing 1 works, and dealing with lazy loading.

Score: 3

Straight JDBC would fit the simplest cases 9 at best.

If you want to stay within Java 8 and OOD then going Hibernate or Hibernate/JPA 7 or any-other-JPA-provider/JPA should be 6 your choice.

If you are more comfortable 5 with SQL then having Spring for JDBC templates 4 and other SQL-oriented frameworks won't 3 hurt.

In contrast, besides transactional 2 control, there is not much help from having 1 Spring when working with JPA.

Score: 3

Hibernate best suits for the middleware 5 applications. Assume that we build a middle 4 ware on top of the data base, The middelware 3 is accessed by around 20 applications in 2 that case we can have a hibernate which 1 satisfies the requirement of all 20 applications.

Score: 1
  1. In JDBC, if we open a database connection 41 we need to write in try, and if any exceptions 40 occurred catch block will takers about it, and 39 finally used to close the connections.

  2. In 38 jdbc all exceptions are checked exceptions, so 37 we must write code in try, catch and throws, but 36 in hibernate we only have Un-checked exceptions

  3. Here 35 as a programmer we must close the connection, or 34 we may get a chance to get our of connections 33 message…!

  4. Actually if we didn’t close the 32 connection in the finally block, then jdbc 31 doesn’t responsible to close that connection.

  5. In 30 JDBC we need to write Sql commands in various 29 places, after the program has created if 28 the table structure is modified then the 27 JDBC program doesn’t work, again we need 26 to modify and compile and re-deploy required, which 25 is tedious.

  6. JDBC used to generate database 24 related error codes if an exception will 23 occurs, but java programmers are unknown 22 about this error codes right.

  7. While we are 21 inserting any record, if we don’t have any 20 particular table in the database, JDBC will 19 rises an error like “View not exist”, and 18 throws exception, but in case of hibernate, if 17 it not found any table in the database this 16 will create the table for us

  8. JDBC support 15 LAZY loading and Hibernate supports Eager 14 loading

  9. Hibernate supports Inheritance, Associations, Collections

  10. In 13 hibernate if we save the derived class object, then 12 its base class object will also be stored 11 into the database, it means hibernate supporting 10 inheritance

  11. Hibernate supports relationships 9 like One-To-Many,One-To-One, Many-To- Many-to-Many, Many-To-One

  12. Hibernate 8 supports caching mechanism by this, the 7 number of round trips between an application 6 and the database will be reduced, by using 5 this caching technique an application performance 4 will be increased automatically

  13. Getting pagination 3 in hibernate is quite simple.

  14. Hibernate has 2 capability to generate primary keys automatically 1 while we are storing the records into database

Score: 0

... In-memory Session ... LazyInitializationException 4 ...

You could look at Ebean ORM which doesn't use 3 session objects ... and where lazy loading 2 just works. Certainly an option, not overkill, and 1 will be simpler to understand.

Score: 0

if billions of user using out app or web 5 then in jdbc query will get executed billions 4 of time but in hibernate query will get 3 executed only once for any number of user 2 most important and easy advantage of hibernate 1 over jdbc.

More Related questions