-->
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: Suggestion: NHibernateUtil.CanInitialize()
PostPosted: Thu Feb 08, 2007 12:27 pm 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

We're using NHibernate 1.2.0beta2 in a WinForms application. The problem I'm going to talk about is the initialization of lazy-loaded collections.

About those collections, there are 3 possibilities when we're trying to access it:
a) the collection is initialized
b) the collection is not initialized, but the object holding the collection is still bound to a (open) session
c) the collection is not initialized and the object holding the collection is no longer bound to a session

Of course (a) is not a problem, everything works fine.
(b) is somewhat a problem, because there's no way to know that the object is still associated to a session.
(c) is a serious problem, because as far as I know, there's no way to know if the collection can be initialized or if the object has to be reassociated to an open session. And trying to reassociate the object with a new session, while there's another open session associated to it, results in an exception.

So for now, we're solving the problem this way:

Code:
public virtual void InitializeFieldCollection()
{
    if (!NHibernateUtil.IsInitialized(this.FieldCollection))
   {
        try
        {
            NHibernateUtil.Initialize(this.FieldCollection);
        }
        catch
        {
       // not associated to an open session
            using (Session session = DataStore.CreateSession())
            {
                session.Lock(this);
                NHibernateUtil.Initialize(this.FieldCollection);
            }
        }
    }
}

(in the above code, Session acts as a wrapper around an ISession-object)

Although it works, I don't like this approach: the try-catch is not very readable and looks messy. Also: Lock() may only be used on unmodified instances, which I'm not sure of at the moment I use it.

So my question is twofold:
- is there any way to know if an object is still associated to an open session ?
- is there a better way to solve this problem, comparing the method we're using above ?

I thought maybe a new method NHibernateUtil.CanInitialize() might help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 11:11 pm 
Beginner
Beginner

Joined: Mon Jan 08, 2007 11:59 pm
Posts: 31
Can't you do session.Contains(object) to see if the object is still associated with the session

Job Samuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 3:47 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
No, because I need to know if the object is associated with a session in the first place. The object is passed from method to method, the session isn't.

The object itself (or at least it's collections) knows it's owned by a session (otherwise NHibernate couldn't throw an exception when you try to reassociate it with another session while the first one is active), I just don't know how to figure out if there's a session and if it's still open.

Thanks for the suggestion anyway.


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.