-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Why can't Hibernate just load objects on demand?
PostPosted: Sat May 13, 2006 11:09 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!
Today I was reading: Why can't Hibernate just load objects on demand?


I find some of its affirmations interesting (I realize this is kind of a sensitive topic, but I just dont feel that the information there is not convincing enough, specially if you have used other ORMs that do have distributed non-transactional lazy loading and I really would like to understand this more (what was the big desing flaw that I missed when working with other ORMS?) (I would also like to state that NHibernate is one of the best ORMs I have used... IMO it is the closest thing to a perfect ORM... if it only had automatic transaction management an non-transactional lazy loading...):

Quote:
If Hibernate would, hidden from the developer and outside of any transaction demarcation, start random database connections and transactions, why have transaction demarcation at all?


That really is a very good question, what exactly it is so good about having transaction demarcation? (wouldn't it be better to have "automatic transactional demarcation"? like this:

Code:
  Customer newCustomer = new Customer(EcoSpace);
  newCustomer.Name = "Air Software Ltd";
  newCustomer.Telephone = "+44 (0)121 243 6689";

  TelephoneNumber alternateTelephoneNumber = new TelephoneNumber(EcoSpace);
  alternateTelephoneNumber.Name := "Home number";
  alternateTelephoneNumber.Number = "+44 (0)121 999 9999";
  newCustomer.ContactDetails.Add(alternateTelephoneNumber)

  EcoSpace.UpdateDatabase();


Wouldnt it be better, if, as we have the Session, and the transactions...we could have an equivalent to ECO's EcoSpace?

(Important Note: I am NOT saying ECO is better than NHibernate, if fact I had to choose between the two for a project, and IMO NHibernate was a better choice, because its mapping capabilities for legacy databases are far superior, and its lower level session/transaction base API gave me more control, and HQL is IMO more powerful than OCL... but not all projects need that level of control, and... specially for new projects that do not need to interoperate with legacy apps, or that will have full control of the database ECO could be a much better alternative because of its easier to use API with automatic transactional demarcation and non-transactional lazy loading)


Quote:
What happens when Hibernate opens a new database connection to load a collection, but the owning entity has been deleted meanwhile?


I guess in that case it should throw an exception... but I don't see this a justification for transaction demarcation... IMHO the same problem can happen with regular "manual session opening an closing"

Quote:
(Note that this problem does not appear with the two-transaction strategy as described above [in the question Can I use two transactions in one Session?] - the single Session provides repeatable reads for entities.)


Yes, but that also means I need to have a session and transaction open during user think time...

Quote:
Why even have a service layer when every object can be retrieved by simply navigating to it?


That is really a problematic question... in fact... the question is my point exactly... why should I bother having a service layer if every object can be retrieved by simply navigating to it? IMHO not having to have a service layer sounds like a good thing... doesn't it?


Quote:
All of this leads to no solution, because Hibernate is a service for online transaction processing (and certain kinds of batch operations) and not a "streaming objects from some persistent data store in undefined units of work"-service. Also, in addition to the n+1 selects problem, do we really need an n+1 transaction and connection problem?


So... that means... that if we want distributed non-transactional lazy loading we should build a streaming objects from some persistent data store in undefined units of work service? is that what other ORMs (like Borland ECO or Apple's EOF) are? who draws the line? I know that the owners of Hibernate are free to choose, but is this decision theoretically supported? I'd like to know how...)


Quote:
The solution for this issue is of course proper unit of work demarcation and design, supported by possibly an interception technique as shown in the pattern here, and/or the correct fetch technique so that all required information for a particular unit of work can be retrieved with minimum impact, best performance, and scalability.


Yes, but OpenSessionInView is only a good idea if you are building a webapplication.. and if you want only one transaction per request/response cycle... if you UI deviates from that... then it is no longer such a good idea...


Quote:
Note: Some people still believe that this pattern creates a dependency between the presentation layer and Hibernate. It does not...


Well, then, why OpenSessionInView isnt a good idea for interactive UIs in smartclients? isn't changing form ASP.NET to WindowsForms a change on the presentation layer? so.. If i built my application to share its bussines logic between a WindowsForms and an ASP.NET presentation and I am going to have problems... then that means I am tied to a particular presentation layer... isn't it? (Maybe OpenSessionInView should be renamed to OpenSessionInWebView...)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 5:37 pm 
Newbie

Joined: Thu Aug 30, 2007 12:02 pm
Posts: 8
Note: Some people still believe that this pattern creates a dependency between the presentation layer and Hibernate. It does not...

You might want to have a look at Dependency injection.


Top
 Profile  
 
 Post subject: Re: Why can't Hibernate just load objects on demand?
PostPosted: Thu Apr 17, 2008 3:10 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:08 pm
Posts: 26
Hi Luxspes, you make some interesting points and have given me some food for thought. To this point in time, I have simply accepted the standard answer to "Why can't Hibernate just load objects on demand?" purely on a matter of faith. I trust that Gavin and Christian have way more experience than me and they know what they are talking about.

However, I wish that someone would support that answer with specific real-life examples of exactly what could go wrong with loading objects on demand.

luxspes wrote:

Quote:
What happens when Hibernate opens a new database connection to load a collection, but the owning entity has been deleted meanwhile?


I guess in that case it should throw an exception... but I don't see this a justification for transaction demarcation... IMHO the same problem can happen with regular "manual session opening an closing"


Luxspes, your guess is the same as mine. That is, I think the lazy load should be automatically attempted and an exception should be thrown if the owning entity has been deleted. I don't understand why this would be a problem.

luxspes wrote:
Yes, but OpenSessionInView is only a good idea if you are building a webapplication.. and if you want only one transaction per request/response cycle... if you UI deviates from that... then it is no longer such a good idea...


Quote:
Note: Some people still believe that this pattern creates a dependency between the presentation layer and Hibernate. It does not...


Well, then, why OpenSessionInView isnt a good idea for interactive UIs in smartclients? isn't changing form ASP.NET to WindowsForms a change on the presentation layer? so.. If i built my application to share its bussines logic between a WindowsForms and an ASP.NET presentation and I am going to have problems... then that means I am tied to a particular presentation layer... isn't it? (Maybe OpenSessionInView should be renamed to OpenSessionInWebView...)


We are using Hibernate in a two-tier HMVC rich client and I have been wondering whether the OpenSessionInView pattern could be adapted to a rich client. Indeed, I think there are some similarities to OpenSessionInView in Christian's Hibernate and Swing demo app. I do think he is onto something with that demo. I think the idea of assigning sessions to HMVC triads (optionally passed on to child triads) could prove to be useful to us.

I think the trick might be in differentiating between: 1) use cases that need long sessions to allow for multiple steps and/or user "think time" and 2) use cases that are short and similar to the request-response model of most interactions in web apps. Our app seems to have both types of use cases. The OpenSessionInView pattern article discusses both the short session and long session variations of the pattern. I think Christian's swing demo framework code could be adapted to support both types of sessions in the same app.

Perhaps someone who is smarter and more articulate than me will some day distill these ideas into a "Open Session in Rich Client View" pattern?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.