File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes When to use ORM? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "When to use ORM?" Watch "When to use ORM?" New topic
Author

When to use ORM?

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

I know how helpful ORM is when you have to do only the CRUD operations in your application. But when it comes to using a huge database, we are better off without ORM.
Now this thing has started to bother me lately.My question is, what is the maximum size of database for which ORM is suitable?
I know, it's suitable for small database. But do we have a rule of thumb, which says that if your database is of this size or going to be of this size, then better leave ORM.

Thanks and Regards,
Vishal


Programming is about thinking, NOT coding
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Well I don't know how large a database should be before ORM is not suitable. But for "big data" there is Hadoop and all those NoSQL database options out there.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
K. Tsang wrote:Well I don't know how large a database should be before ORM is not suitable. But for "big data" there is Hadoop and all those NoSQL database options out there.


Thanks, but I wanted to know about ORM. I did not ask What to use instead of ORM. But still many thanks to you, as it was a valuable information
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Size of the database is completely irrelevant when deciding whether or not the use of an ORM is appropriate.

I am going to quote from an interview with Gavin King (founder of the Hibernate project)

I think this sums it up fairly well.

Why, where and when should someone use Hibernate?

You should use Hibernate if you have a nontrivial application (definition of nontrivial varies, but I usually think of Hibernate being less applicable to applications with only ten tables or so) that use an object-oriented domain model. Not every application needs a domain model, so not every application needs ORM. But if your application does a lot of business logic - rather than just displaying tabular data on a webpage - then a domain model is usually a good thing.

Hibernate really starts to shine in applications with very complex data models, with hundreds of tables and complex interrelationships. For this kind of application, Hibernate will take away a huge amount of coding effort (perhaps up to 25%, for some applications) and will result in an application that performs better than the alternative handcrafted JDBC. This is possible because some kinds of performance optimizations are very difficult to handcode: caching, outer-join fetching, transactional write-behind, etc.


Here is the link to the interview if you are interested.
http://www.javaperformancetuning.com/news/interview041.shtml


[How To Ask Questions][Read before you PM me]
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Thanks Bill for the link. But this has made me more curious.

Bill Gorder wrote: Size of the database is completely irrelevant when deciding whether or not the use of an ORM is appropriate.

Does this mean, there's no performance constraint for Hibernate based on the data size.

Personally , I like Hibernate not only because it is Developer friendly, but also pretty fast (use Cached Data). But one thing I am sure of is that, if data size is big then initialization of Hibernate is going to be time consuming. Apart from that, is there any other hit?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Vishal Shaw wrote:

Personally , I like Hibernate not only because it is Developer friendly, but also pretty fast (use Cached Data). But one thing I am sure of is that, if data size is big then initialization of Hibernate is going to be time consuming. Apart from that, is there any other hit?


Remember that the initialization does not load the entire data, if that's what you mean by the data size. All it does is maps the Java objects to the DB tables which is a one time activity and even if the number of tables (irrespective of how much data they hold) is large I haven't yet seen anyone complain that this is a major issue in terms of time taken. It might be a few seconds and as I said it will be a one time thing.


[My Blog] [JavaRanch Journal]
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Thanks Jaikiran for your reply. Just tell me this one thing:

When people say "You should not use Hibernate, as you have so huge data", are they ignorant?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Vishal Shaw wrote:

When people say "You should not use Hibernate, as you have so huge data", are they ignorant?

I actually haven't read it myself so I can't say in what context they say that. Do you have some reference to that?
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Jaikiran Pai wrote:
Vishal Shaw wrote:

When people say "You should not use Hibernate, as you have so huge data", are they ignorant?

I actually haven't read it myself so I can't say in what context they say that. Do you have some reference to that?


For example you can check this out.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Ok well two things about that post. It is not talking about the size of the database but rather talking about doing very large batch (insert or update) operations with Hibernate.

1.) This can be done with Hibernate, but the poster in the link you gave is not doing it correctly.
2.) Despite my comment in #1, I would still choose straight JDBC over hibernate for very large batch operations. If you check the reference documentation it goes over how to correctly handle batch processing with Hibernate. This works fine especially for small batches, but batch processing is not really what ORM's are for, which is why I would not use them for this purpose.

To Jaikiran's point the 2nd level cache, query cache etc are to improve performance not hurt it. This is fully in the developers control. If used improperly you will get worse performance than if you had not used the ORM. Hibernate used properly will result in performance gains, Hibernate used improperly will result in losses. Typically these scenarios are not the fault of the framework but the developer.
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Bill Gorder wrote:Ok well two things about that post. It is not talking about the size of the database but rather talking about doing very large batch (insert or update) operations with Hibernate.

1.) This can be done with Hibernate, but the poster in the link you gave is not doing it correctly.
2.) Despite my comment in #1, I would still choose straight JDBC over hibernate for very large batch operations. If you check the reference documentation it goes over how to correctly handle batch processing with Hibernate. This works fine especially for small batches, but batch processing is not really what ORM's are for, which is why I would not use them for this purpose.

To Jaikiran's point the 2nd level cache, query cache etc are to improve performance not hurt it. This is fully in the developers control. If used improperly you will get worse performance than if you had not used the ORM. Hibernate used properly will result in performance gains, Hibernate used improperly will result in losses. Typically these scenarios are not the fault of the framework but the developer.


Thanks, got your point. Since Hibernate uses one object for each db record, so executing a batch process with a large dataset will result in more memory being consumed, right?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: When to use ORM?