-->
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.  [ 6 posts ] 
Author Message
 Post subject: A few questions.
PostPosted: Sat Feb 14, 2004 10:54 am 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
Lazy Initialization

Let's say for simplicity that I have the following

Class A
Collection bs

Class B
Collection cs

Class C
Collection ds

Class D

A) So I basically have a chain of one-to-many relationships, all of which are using lazy initialization. At what point should each Collection actually get loaded? It appears, from my testing, that as soon as I reference the Collection of bs, all subsequent collections also get loaded. I would have guessed that each related Collection would not be loaded until it was explicitly referenced. So, I am not sure if this is the way it is supposed to work, or I am doing something wrong here.


B) In some cases, referencing these collections may throw a LazyInitializationException. Because it is a RuntimeException, there is no real graceful/pretty way to recover from this. Is there a write-up somewhere that explains why this is this way, or why the DAO's do not throw an exception? I have seen many posts on this subject, but none that really explained things well.



net.sf.hibernate.UserType

I have created a few of these, but I have not seen any documentation on how it is referenced internally. Is it safe to use member variables in these classes? Would/should there be a case that any of the functions ( ie nullSafeSet, nullSafeGet ) are synchronized?


Thanks in advance for any information, the help is much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2004 8:57 pm 
Newbie

Joined: Sun Dec 28, 2003 4:35 pm
Posts: 13
Hello

A: Are really all the one-to-manies set to lazy?

B:I suppose you tried to access a lazy collection after having closed the session. And the Daos are always accessed from within a session.

Salut, Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 7:45 am 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
A: Yes they are all set to lazy. Is this not supposed to happen?

B:No, I did not try to access them outside the session. This will happen without changing code. If the relationship expects there to be rows there, but they are not, then this exception is thrown.

It will also throw this exception if it looses connectivity with the database. Try unplugging your network cable while a look up is being done. The message is very concise. but it is still a RunTimeException.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 1:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
A. Subsequent lazy one-to-many are not loaded unless they are explicitly requested.

B.
Quote:
If the relationship expects there to be rows there, but they are not, then this exception is thrown.

What d you mean

Quote:
Try unplugging your network cable while a look up is being done. The message is very concise. but it is still a RunTimeException.

Interesting. You're saying that unplugging the network while accessing a lazy collection in an opened session throws a LazyInitializationException. We could have a subclass of LazyInitializationException to be more precise, but it 'll stay a RT. The collection API does not declare checked exceptions.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 2:54 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
A: I will have to look into this more to see if I can see a reason for this happening. While stepping through the code and hibernate logging set to debug, I can see many selects on the first reference to a collection ( those including subsequent collections ) and nothing after I actually reference the subsequent collections.

B: Using the above example

In A I have a foreign key defined as not null; meaning it is a true one-to-many relationship.

In B I have a foreign key defined as nullable; meaning it is actually a zero-to-many relationship.

What would you expect to happen if in either case either the foreign key was null, or its value did not actually exist in the foreign table?

I understand that the first scenario is actually an error, but it can happen. I am just trying to get a good understanding of how things work and what to expect.


Yes, unplugging the network will throw a LazyInitializationException, it looks aas though it wraps a JDBCException. What was the reasoning behind the collection API not declaring any checked exceptions? It would seem like the natural thing to do, but I am sure you all had good reasoning behind it.


Thanks again for the replies.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 6:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
tzman wrote:
In A I have a foreign key defined as not null; meaning it is a true one-to-many relationship.

In B I have a foreign key defined as nullable; meaning it is actually a zero-to-many relationship.

What would you expect to happen if in either case either the foreign key was null, or its value did not actually exist in the foreign table?

I understand that the first scenario is actually an error, but it can happen. I am just trying to get a good understanding of how things work and what to expect.


Hibernate will get lost. I can't remember the exact error message. Just try it, you should get an HibernateException.


tzman wrote:
Yes, unplugging the network will throw a LazyInitializationException, it looks aas though it wraps a JDBCException. What was the reasoning behind the collection API not declaring any checked exceptions? It would seem like the natural thing to do, but I am sure you all had good reasoning behind it.

Hehe, this is core java api (java.util.Collection). Ask sun to add an HibernateException ;-)

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.