-->
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: Why NHibernate Fills the Collection Using Projection When ..
PostPosted: Fri Oct 05, 2007 11:35 am 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Why NHibernate Fills the Collection Using Projection When I Said Lazy = "True".

I am having a really hard time with NHibernate and Lazy Loading and I know many people are having the same issues. One of the other problems is that when I tell NHibernate that I was to load Users as lazy load it stills fill the Users list of the root object. This way I cannot even check for null and it always throws the NULL Exception.

The idea of having to keep the Session open while accessing the lazy load is worthless as why should I leave the session open and waste my resources.

In the code below _users is never null although lazy="true". This is because NHibernate fills it up with some projection and initialize the object.

public virtual IList<User> Users
{
get {

if (_users == null)
{
_users = UserManager.GetByExamId(this.Id);
}
return new ReadOnlyCollection<User>(_users);

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 11:45 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Don't do any kind of tricks (i.e. checking for null/not null, wrapping, etc.) in your getters and setters. NHibernate depends on them being well-behaved.


Top
 Profile  
 
 Post subject: RE:
PostPosted: Fri Oct 05, 2007 11:48 am 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Not sure what you meant there!

But when I only do this using NHibernate configurations then I get LazyLoadException. Here are my configuration files:

This is Exam.hbm.xml:

<bag name="Users" access="nosetter.camelcase-underscore" lazy="true" cascade="save-update" table="UserExams" >

<key column="ExamID" />
<many-to-many class="User" column="UserID" />
</bag>

Here is the Users property in the Exam.cs class.

public virtual IList<User> Users
{
get {


return new ReadOnlyCollection<User>(_users);

}
}

And here is the code:

Exam exam = ExamManager.GetById(55);

// Fails on the line below::::
Console.WriteLine(exam.Users[0].FirstName);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 1:22 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I meant this:
Quote:
get { return new ReadOnlyCollection<User>(_users); }

should be just:
Code:
get { return _users; }


or use access="field.blabla", not nosetter. Basically, don't confuse NHibernate.

And another thing, lazy doesn't mean the property will get initialized to null, it means the property will get initialized to a proxy (a collection proxy in this case).


Top
 Profile  
 
 Post subject: Same Error!!
PostPosted: Fri Oct 05, 2007 1:54 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Hi,

I removed the ReadOnlyCollection but it still gives me the same error.

There are just too many constraints when using NHibernate. It is like NHibernate is saying to me "It is my way or the highway".

No flexibility!


Top
 Profile  
 
 Post subject: One to many relationship
PostPosted: Fri Oct 05, 2007 1:55 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
Can you show me how to make a one to many relationship where a Users collection is lazy loaded?


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.