Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advantage of Hibernate over JDBC

 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is where is the advantage of code reduction in Hibernate when compared to JDBC? I am aware of other advantages of Hibernate like lazy loading, transaction handling, caching but just don’t understand the code reduction in Hibernate because I don’t see it.

Ok. Lets take an example. Lets assume there are 100’s of tables say Table1, Table2 ….Table100. The tables contain details of employees.

And say Table1 contains ‘a1’,’b1’,’c1’…’z1’ 
Table2 contains ‘a2’,’b2’,’c2’………’z2’
and so on till Table100

Now let see the implementation in JDBC and Hibernate. Lets see where the advantage is, in Hibernate(Unfortunately I don't seem to find any reduction in code).

JDBC Code:


Hibernate Code:


Here I see JDBC code being much shorter than Hibernate code. So what am I missing?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15639
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You make it seem like you have to do much less when you program with JDBC than when you program with Hibernate. But you forgot that with JDBC you also need extra code to open the connection, create a statement, execute the statement, get the results out of the ResultSet etc. So the comparison between your two code examples is not really fair.

Also, look at the code in your two methods addNameToTable1 and addNameToTable2. The code is almost exactly the same, only the table type is different. There's no need to repeat code like that, it could be written in a much better way without so much copy-and-paste code.

When you program in Java, you normally want to work with objects. You have objects that contain fields that contain your data. For example, you have an Employee object, and what you want to do is save that object in the database. When you program with JDBC, you have to manually create a statement, get all the fields of the object one by one and set them in the statement, and then execute the statement. Or if you want to retrieve an Employee object you execute a query and then for each row you have to get the fields one by one and put them in a new Employee object. That's all very cumbersome and low-level. When you would use Hibernate, you can just tell Hibernate: store this Employee object, or retrieve the Employee object with this name. You would not need to do all that low-level field-by-field stuff.

Programming with plain JDBC versus programming with Hibernate is like programming in assembly language versus programming in a higher-level language like Java.
 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that in Java we work with objects which contains fields with data in them. But then Hibernate has its own set of headaches like creating sessionFactory, sessions and transactions.

So how exactly can we say there is code reduction in Hibernate apart from features Hibernate has like lazy loading, caching etc?

Please explain
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't you asked this before?
http://www.java-forums.org/hibernate/88089-advantage-hibernate-over-jdbc.html
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who is the "we" which supposedly says this?
 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@db
I asked in the other forum but everyone was asking to take an example with many tables. So I created an example which contained 100's of tables. But I am facing problem when I put posts which are big, in that forum. Hence I posted in this forum.

@Ulf
By "we" I mean Java developers.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15639
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Mohandos wrote:I agree that in Java we work with objects which contains fields with data in them. But then Hibernate has its own set of headaches like creating sessionFactory, sessions and transactions.

When you program in plain JDBC you will also have to think about opening and closing database connections, possibly using a database connection pool, creating statements, getting and closing result sets, and transactions. Hibernate is not more difficult than plain JDBC in that regard.

J Mohandos wrote:So how exactly can we say there is code reduction in Hibernate apart from features Hibernate has like lazy loading, caching etc?

I already gave an explanation in my answer above.

Suppose you want to get a list of customers which have registered a year ago or longer. In plain JDBC, you would do something like this.

Do you see how, for the result set, you have to get each and every field and manually build up a Customer object? It's very cumbersome. With Hibernate, you can let Hibernate do all the work.

Do you see how that is much shorter and how easy it is to read and understand?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic