File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Which one is faster Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Which one is faster" Watch "Which one is faster" New topic

Which one is faster

Pankaj Patro
Ranch Hand

Joined: Apr 20, 2005
Posts: 35
Hibernate Vs JDBC

As comapre to time only which one is faster.

Thanks & Regards,
Pankaj Patro
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Well, Hibernate does a lot more than JDBC does so probably JDBC. Mind you, Hibernate probably performs better than poorly written JDBC code. This is just conjecture though; if you want to find out write a test and see. Of course you'd never select an ORM if your only metric is performance. Hibernate (as with most ORMs) performs well enough to be useful.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

It really depends. There are definitely ways to make Hibernate run faster, like when it gets the data from cache, JDBC could not do this. Also the way that Hibernate works with batch statements and prepared statements can better utilize JDBC underneath than most people think about when they do JDBC themselves.

The big key is that if you use JDBC code, then about 30% of your code will be JDBC code, whereas with Hibernate that would be removed, you would still have your DAO code to create queries, but in general it is much easier to read, and you don't have to deal with Object arrays as result sets as you get with JDBC.
This paragraph has nothing to do with performance, just some key points.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
smectite wo

Joined: Feb 05, 2007
Posts: 4
Hi, Guys,

just a couple of questions want to ask:

1. You talked about that hibernate automaticly batches the statement, does hibernate change the order of the statements in one single batch it forms.
I ran into the problem in EJB2.0 with weblogic. When I tried to deleted a record with a unique constraint and then insert a record with the same unique constraint in one single transaction, I got "unique constraint violation". It seems that weblogic batches the statements but changes the query order or fire them simutaneously. Does hibernate have the mechanism to stop sth like this.
2. Does hibernate do the result set cahching? or it only does object instance caching?

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

When mapped correctly, then Hibernate will run statements in the correct order.

Hibernate stores the mapped object in its persistence context, not the ResultSet object.

smectite wo

Joined: Feb 05, 2007
Posts: 4
Thanks. Mark.

To answer which one is faster.

Since most orm using java reflection to populate the data. It is slower than to manupilate the result set directly. Then again as Mark said, orm can do optimization for you.
personally, I do not htink that you need hibernate or ejb2.0/3.0 if your program has no heavy load, not distributed, and it dosn't need transaction, data concurrency and integrity.
For read-only data, I prefer ibatis.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Originally posted by smectite wo:
Since most orm using java reflection to populate the data. It is slower than to manupilate the result set directly...For read-only data, I prefer ibatis.

Well, I am a big fan of iBatis. But it uses reflection also. The difference is that you are writing the SQL for iBatis. Hibernate generate *most* of the SQL for you in *most* cases.

I find this thread odd because Hibernate uses JDBC. So I suppose the OP's question was just worded badly. It probably should have been:

"Hibernate Vs straight JDBC"

Though I suppose that was implied and I am just overthinking it.

GenRocket - Experts at Building Test Data
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

"smectite wo", welcome to the JavaRanch.

We're a friendly group of people, but we do require members to have valid display names.

Display names must contain your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names will be deleted.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

In theory, JDBC should be faster, just as in theory assembly language is faster than compiled high-level languages. And, at the micro level, both are usually true. At the macro level, however - and, more importantly, in measured studies - ORM frameworks such as Hibernate and JDO often provide much better performance. That's because, although the frameworks are running generalized performance code, it's usually not worth all the labor involved in hand-tuning low-level code, so the handwritten stuff isn't performant at the macro level.

YMMV, of course. When it really matters, it's worth setting up metrics and experimenting. Most of the time, the reduction in coding and debugging time alone makes ORM worth the effort. Especially the reduction in debugging time.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Which one is faster
It's not a secret anymore!