aspose file tools*
The moose likes Spring and the fly likes Spring JDBC Vs Hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring JDBC Vs Hibernate?" Watch "Spring JDBC Vs Hibernate?" New topic
Author

Spring JDBC Vs Hibernate?

Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Dear All,

I want to know if we talk about Spring JDBC and Hibernate,
which one is better regarding performance. I have heard people
prefer Spring JDBC over hibernate, can someone tell me the real
issues behind as i am new to the technology.

Ben


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3261
Hibernate is an O/R mapping tool.O/R mapping is a technique of mapping data representation from an object model to a SQL based relational model.

O/R mapping is well suited for read --> modify --> write centric applications and not suited for write centric applications (i.e. batch processes with large data sets like 5000 rows or more) where data is seldom read. Although this was generally true of many earlier O/R mapping frameworks, most today (including latest Hibernate) allow for efficient ways of performing large batch style write operations. O/R mapping tools/frameworks allow you to model inheritance, association and composition class relationships. O/R mapping tools work well in 80-90% of cases.

Use basic database features like stored procedures, triggers etc, when O/R mapping is not appropriate. Keep in mind that no one size fits all solution. Always validate your architectural design with a vertical slice and test for performance. Some times you have to handcraft your SQL and a good O/R mapping (aka ORM) tool/framework like should allow that. O/R mapping tools/frameworks allow your application to be:

�Less verbose (e.g. transparent persistence , Object Oriented query language , transitive persistence etc)
�More portable (i.e. vendor independence due to multi dialect support )
�More maintainable (i.e. transparent persistence, inheritance mapping strategies, automatic dirty checking etc).

Takes care of much of the plumbing like connection establishment, exception handling, configuration etc. You can often leverage the framework�s strategies and capabilities to get efficiencies. Also provides support for eager fetching, lazy loading (i.e. using proxy objects), caching strategies and detached objects (no DTOs required). Hibernate is a popular O/R mapping (aka ORM) framework, which provides above mentioned benefits and features.


500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
pham thanh tung
Greenhorn

Joined: Aug 15, 2011
Posts: 7
@arulk pillai: Thank you for adviced, and i want ask a question, so what framework best for
5000 rows or more
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
From my experience using Hibernate have a lot of advantages, but less of control and performance advantages over JDBC.


See below for more info:


Learn the Hibernate advantages and disadvantages and decided when to user Hibernate.
Hibernate advantages and disadvantages

In this section we will discuss Hibernate advantages and disadvantages, and see why we should or shouldn't use Hibernate. Hibernate is an ORM tool that automatically maps your domain object to the relational database. It helps to developers to quickly write database access program and become productive. But we will see the advantages and disadvantages of Hibernate ORM tool.

Java developers of all over the world is mostly using Hibernate or JPA (with Hibernate) for developing enterprise web applications. There are lots of material and tutorials on Hibernate on the web. As a developer if you are stuck some where in your programming, you can easily find the solution of your problem on the web. You can even download and learn Hibernate easily from our Hibernate Tutorial section.

Let's discuss the advantages and disadvantages of Hibernate

Advantages of Hibernate

Hibernate is better then plain JDBC: You can use Hibernate which generates the SQL on the fly and then automatically executes the necessary SQL statements. This saves a lot of development and debugging time of the developer. Writing JDBC statement, setting the parameters, executing query and processing the result by hand is lot of work. Hibernate will save all tedious efforts.

Mapping of Domain object to relational database: Hibernate maps your domain object with the relational database. Now you can concentrate on your business logic rather than managing the data in database.

Layered architecture: Hibernate is layers architecture and you can use the components as per your application need.

JPA Provider: Hibernate can work as JPA provider in JPA based applications.

Standard ORM: Hibernate is standard ORM solutions and it also supports JPA.

Database Independent: Hibernate is database independent and you can use any database of your choice.

Caching Framework: There are many caching framework that works with Hibernate. You can use any one in your application to improve the performance of your application.


Disadvantages of Hibernate

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.


Source: RoseIndia


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
pham thanh tung
Greenhorn

Joined: Aug 15, 2011
Posts: 7
thank you. I am a newbie of java.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

I disagree with this one statement. I can actually make Hibernate run much faster than pure JDBC for some use cases. This is because sometimes Hibernate doesn't even have to go to the database, where every JDBC call will always go to the database.

Example scenario.

I have a use case that calls 10 different update statements where I just change the domain object that was loaded through Hibernate. Hibernate has automatic change detection, so it creates 10 update statements but does not send them down to the database until transaction commit time. So at this point there was only one call to the database to load the data at the start. somewhere in the use case near the end before committing the transaction an exception occurs that will roll everything back. Well for Hibernate, it doesn't have to send those updates to the database. So I only have one call to the database in the loading of data.

In this scenario, with JDBC. I would have sent 11 (1 for loading the data and 10 for each update statement) different calls to the database, and then one more at the end to send it a rollback to rollback all the updates.

so 12 calls to the database in JDBC for the use case failure, versus just 1 or 2 with Hibernate. I definitely can tell you that Hibernate will be faster in that scenario.

Mark

12 calls


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
pham thanh tung
Greenhorn

Joined: Aug 15, 2011
Posts: 7
Ah, thank you. Current i have a problem, with complex query statement so what i can handler? I want join multi-table, or multi-param for multi-condition.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

pham thanh tung wrote:Ah, thank you. Current i have a problem, with complex query statement so what i can handler? I want join multi-table, or multi-param for multi-condition.


That is an SQL question, not a Spring question.

Mark
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
pham thanh tung wrote:Ah, thank you. Current i have a problem, with complex query statement so what i can handler? I want join multi-table, or multi-param for multi-condition.


Hibernate can handles these scenarios, if you look in Hibernate resources you'll find many features Hibernate can handles multiple tables join on the Domain object. You can translate a complex query to Hibernate by using features it provided.

It would be a pain for Hibernate in these type of scenarios especially Parent and Child object relationship, from the experience you'll have a hard time troubleshooting on a problem that Hibernate update the Parent instead of the Child object. There is a way to get around the problem, it just depend on how much knowledge and experience you have with Hibernate.

Having said all that, make sure you know what you're doing when using Hibernate so, it won't haunt you down the road.



pham thanh tung
Greenhorn

Joined: Aug 15, 2011
Posts: 7
Mark Spritzler wrote:
pham thanh tung wrote:Ah, thank you. Current i have a problem, with complex query statement so what i can handler? I want join multi-table, or multi-param for multi-condition.


That is an SQL question, not a Spring question.

Mark

Ah, Sorry, because i so focused on above answer.
Yosi Pramajaya
Ranch Hand

Joined: Jun 30, 2011
Posts: 46

Prefer Hibernate to Spring JDBC.
But, Hibernate can be integrated with Spring CORE.

For example : Injecting data to Hibernate's SessionFactory and it's Database's data to create a portal between Application and Database.


Independent Programmer - Technology Enthusiast - JUG Leader from JUG Joglosemar
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Nitin Gaur wrote:Came across this thread while searching JDBC vs JPA discussions.

I have batch application that fetches customer data (from multple tables), get it processed from an external app, stores the results back in same database (different set of tables). This batch has to run for around 100 thousand (100,000) customers.
We thought to use JPA with openJPA (sorry no hibernate) though no solid reason but it is proving to be slower than expected. Now, conteplating to switch to JDBC. Have not done JDBC test yet but inviting point of view if it is right approach?

-Nitin


My Suggestion would be to use Spring Batch. Check out some example on net.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

You should have posted in a new thread, instead of resurrecting this older thread. I will move your post out into a new thread.

I also agree that you should use Spring Batch.

Also, again JPA or JDBC becomes slow when the developer does things improperly. So you can make that batch run quicker with JPA, but you have to understand JPA as an expert to know that you shouldn't load all 100K records in one go in JPA. Do it in batches/chunks, which is why Spring Batch will really help.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Nitin Gaur,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
 
subject: Spring JDBC Vs Hibernate?