I want to use query over to give me back an object
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
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
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
'((new System.Collections.Generic.Mscorlib_CollectionDebugView<Domain.PlannerTask>(result)).Items[0].Course).CoursePermissions' threw an exception of type 'NHibernate.ObjectNotFoundException'
Try 3
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();
SELECT this_.TaskId as y0_,
this_.TaskName as y1_,
this_.DueDate as y2_,
this_.Description as y3_,
this_.TaskId as y4_,
this_.TaskId as y4_,
this_.CourseId as y4_
FROM Tasks this_
Result:
CoursePermissions = '((Castle.Proxies.CourseProxy)((new System.Collections.Generic.Mscorlib_CollectionDebugView<PlannerTask>(result)).Items[0].Course)).CoursePermissions' threw an exception of type 'NHibernate.ObjectNotFoundException'
try 4
Try 4
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.Course).WithAlias(() => plannerTask.Course))
.TransformUsing(Transformers.AliasToBean<PlannerTask>())
.List<PlannerTask>().ToList();
SELECT this_.TaskId as y0_,
this_.TaskName as y1_,
this_.DueDate as y2_,
this_.Description as y3_,
this_.CourseId as y4_
FROM Tasks this_
Works. I can access everything in the course object.
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.
session.QueryOver<Task>().Fetch(t => t.PersonalTaskReminders).Eager...
Any eagerly fetched entities will be fetched in one query. – Ingar