-->
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.  [ 2 posts ] 
Author Message
 Post subject: Failed to Initialize a Lazy Collection
PostPosted: Thu Oct 04, 2007 4:40 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
I have two entities User and Exam. The relationship between them is many-to-many since a single User can take many exams and a single Exam is given to many users.

I have a exam persisted in the database and now I want to fetch the exam but not the Users of the exam. So, I have a mapping in my Exam.hbm.xml something like this:

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

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


The ExamManager.GetById method fetches the Exam

public static Exam GetById(int id)
{
Exam exam = null;

using (ISession session = sessionFactory.OpenSession())
{
exam = session.Get<Exam>(id);

// initialize the collection
NHibernateUtil.Initialize(exam.Users);

}

return exam;
}

And here is the code that is blowing off:

Exam exam = ExamManager.GetById(55);


foreach (User u in exam.Users)
{

// blows when I try to access the FirstName
Console.WriteLine(u.FirstName);


}


The exception is "Failed to Initialize a Lazy Collection". The inner exception is "Object Reference not set to an instance of an object".

Here is the Exam.cs:

private IList<User> _users = new List<User>();

And the Users property:

public IList<User> Users
{
get {

return new ReadOnlyCollection<User>(_users); }
}


Thanks!


Top
 Profile  
 
 Post subject: Strange Behaviour
PostPosted: Thu Oct 04, 2007 4:45 pm 
Newbie

Joined: Sun May 29, 2005 3:55 pm
Posts: 13
I debugged the application and when I debug it then it does not throw any error. But when I run it then it throws the errors.

I mean in debug mode It does not even go in the catch block.


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