Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem: failed to lazily initialize a collection of role
PostPosted: Mon Oct 24, 2011 10:11 am 
Newbie

Joined: Mon Oct 24, 2011 10:04 am
Posts: 2
I'm currently learning ASP.NET with MVC3 using NHibernate and FluentNHibernate. I made myself a simple one-to-many test to try and retrieve a one to many relationship and put this into a poco structure.

As an example, I have a game entity and a developer entity, one developer can have multiple games and a game only has one developer.

I am basing my current views around the developer, and wish to see a list of games that were developed by them. When viewing the details of a single developer in the browser, I am getting the error:

Initializing[OneToManyTest.Models.Objects.Developer#1]-failed to lazily initialize a collection of role: OneToManyTest.Models.Objects.Developer.Game, no session or session was closed.

I know what this means, namely that the Game data cannot be retrieved once the session was closed, which is why I am trying eager loading. This however, doesn't seem to work. The query NHibernate executes is ok, but my code will not properly fill the POCO objects.

The developer and game POCO classes look like this:
Code:
    public class Developer
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
       
        private IList<Game> game = new List<Game>();
        public virtual IList<Game> Game
        {
            get { return game; }
            set { game = value; }
        }
    }

    public class Game
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int DeveloperId { get; set; }
        public virtual Developer Developer { get; set; }
    }


The fields of these classes match one-on-one with the database structure and column naming, except for the Developer field in the Game class (which is ofcourse non-existant in the database)

I have the following controller method for retrieving the data of a single developer:

Code:
        public ActionResult Details(int id)
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                //var developer = session.Get<Developer>(id);
                var query = session.CreateCriteria<Developer>()
                        .SetFetchMode("Game", FetchMode.Eager)
                        .CreateCriteria("Game");
                var developer = query.List<Developer>().FirstOrDefault<Developer>();
                return View(developer);
            }
        }


The mapping to the data looks as follows:

Code:
    public class DeveloperMap : ClassMap<Developer>
    {
        public DeveloperMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasMany<Game>(x => x.Game).Access.CamelCaseField(Prefix.None).KeyColumn("DeveloperId");
        }
    }
    public class GameMap : ClassMap<Game>
    {
        public GameMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            References<Developer>(x => x.Developer).Column("DeveloperId");
        }
    }


Furthermore the view fails when I try to access the Game field of a Developer object as such:

Code:
@model OneToManyTest.Models.Objects.Developer

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

@Model.Name

@Model.Game.Count



The error occurs on the
Code:
@Model.Game.Count
line

I have tried a lot of googling, fiddling and am completely out of options, so I hope that anyone would be willing to spend some time in this and help me :). If desired, i could send you a zip of the solution + create table queries.

Thanks a lot for your time!


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

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.