-->
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.  [ 1 post ] 
Author Message
 Post subject: How to select an object back from a nhibernate query over?
PostPosted: Sat May 07, 2011 2:21 pm 
Newbie

Joined: Sat May 07, 2011 2:19 pm
Posts: 3
Hi

I want to use query over to give me back an object

Code:
     public class TaskMap : ClassMap<Task>
        {
            public TaskMap()
            {
                Table("Tasks");
                Id(x => x.TaskId);
                Map(x => x.TaskName).NvarcharWithMaxSize().Not.Nullable();
                Map(x => x.Description).NvarcharWithMaxSize();
                Map(x => x.DueDate).Not.Nullable();
                HasMany(x => x.PersonalTaskReminders).Inverse();
                HasMany(x => x.TaskReminders).Inverse();
                References(x => x.Course).Not.Nullable();
                HasMany(x => x.CompletedTasks);
     
            }
        }
       
       
   
    [Serializable()]
    public class Task
    {
        public virtual int TaskId { get; private set; }
        public virtual string TaskName { get;  set; }
        public virtual string Description { get; set; }
        public virtual DateTime DueDate { get; set; }
        public virtual IList<PersonalTaskReminder> PersonalTaskReminders { get; set; }
        public virtual IList<TaskReminder> TaskReminders { get; set; }
        public virtual IList<CompletedTask> CompletedTasks { get; set; }
        public virtual Course Course { get; set; }

        public Task()
        {
            PersonalTaskReminders = new List<PersonalTaskReminder>();
            TaskReminders = new List<TaskReminder>();
            CompletedTasks = new List<CompletedTask>();
        }

    }
         public class PlannerTask
    {
        public int TaskId { get; set; }
        public string TaskName { get; set; }
        public string Description { get; set; }
        public DateTime DueDate { get; set; }
        public List<PersonalTaskReminder> PersonalTaskReminders { get; set; }
        public List<TaskReminder> TaskReminders { get; set; }
        public Course Course { get; set; }
        public bool IsCompleted { get; set; }


    }


**Try 1**

Code:
    Task tAlias = null;
            PlannerTask plannerTask = null;
            List<PlannerTask> result = session.QueryOver<Task>(() => tAlias)
                .Select(x => x.Course).TransformUsing(Transformers.AliasToBean<PlannerTask>())               
                .List<PlannerTask>().ToList();

result: 188 records with all null for course object.


**Try 2**
Code:
    Task tAlias = null;
                PlannerTask plannerTask = null;
                List<PlannerTask> result = session.QueryOver<Task>(() => tAlias)
                             .Select(Projections.Property(() => tAlias.TaskId).WithAlias(() => plannerTask.TaskId),
                             Projections.Property(() => tAlias.TaskName).WithAlias(() => plannerTask.TaskName),
                             Projections.Property(() => tAlias.DueDate).WithAlias(() => plannerTask.DueDate),
                             Projections.Property(() => tAlias.Description).WithAlias(() => plannerTask.Description),
                             Projections.Property(() => tAlias.PersonalTaskReminders).WithAlias(() => plannerTask.PersonalTaskReminders),
                             Projections.Property(() => tAlias.TaskReminders).WithAlias(() => plannerTask.PersonalTaskReminders),
                             Projections.Property(() => tAlias.Course).WithAlias(() => plannerTask.Course))
                    .TransformUsing(Transformers.AliasToBean<PlannerTask>())               
                    .List<PlannerTask>().ToList();


Result: 188 courses with all properties having pretty much the same error
Code:
    '((new System.Collections.Generic.Mscorlib_CollectionDebugView<Domain.PlannerTask>(result)).Items[0].Course).CoursePermissions' threw an exception of type 'NHibernate.ObjectNotFoundException'

**Try 3**

Code:
     Task tAlias = null;
            PlannerTask plannerTask = null;
            List<PlannerTask> result = session.QueryOver<Task>(() => tAlias)
                 .Select(Projections.Property(() => tAlias.TaskId).WithAlias(() => plannerTask.TaskId),
                 Projections.Property(() => tAlias.TaskName).WithAlias(() => plannerTask.TaskName),
                 Projections.Property(() => tAlias.DueDate).WithAlias(() => plannerTask.DueDate),
                 Projections.Property(() => tAlias.Description).WithAlias(() => plannerTask.Description),
                 Projections.ProjectionList().Add(Projections.Property(()=> tAlias.PersonalTaskReminders).WithAlias(() => plannerTask.PersonalTaskReminders)),
                 Projections.ProjectionList().Add(Projections.Property(()=> tAlias.TaskReminders).WithAlias(() => plannerTask.PersonalTaskReminders)),
                 Projections.Property(() => tAlias.Course).WithAlias(() => plannerTask.Course))
                .TransformUsing(Transformers.AliasToBean<PlannerTask>())               
                .List<PlannerTask>().ToList();


Result:

Code:
    CoursePermissions = '((Castle.Proxies.CourseProxy)((new System.Collections.Generic.Mscorlib_CollectionDebugView<PlannerTask>(result)).Items[0].Course)).CoursePermissions' threw an exception of type 'NHibernate.ObjectNotFoundException'



It seems to have a problem with

tAlias.PersonalTaskReminders and tAlias.TaskReminders. If I remove these Course will render fine.

I don't understand why.


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.