Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use ORM?

 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
K. Tsang
Bartender
Posts: 3440
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic