gpt4 book ai didi

nhibernate - 如何使用 nhibernate queryover "Select"

转载 作者:行者123 更新时间:2023-12-04 00:23:08 25 4
gpt4 key购买 nike

我想使用查询返回给我一个对象

 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; }


}

尝试 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();

结果:188 条记录,类(class)对象全为空。

尝试 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();

结果:188 门类(class)的所有属性都具有几乎相同的错误
'((new System.Collections.Generic.Mscorlib_CollectionDebugView<Domain.PlannerTask>(result)).Items[0].Course).CoursePermissions' threw an exception of type 'NHibernate.ObjectNotFoundException'

尝试 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_

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

尝试 4

尝试 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_

作品。我可以访问类(class)对象中的所有内容。

好像有问题

tAlias.PersonalTask​​Reminders 和 tAlias.TaskReminders。如果我删除这些类(class)将呈现良好。

我不明白为什么。

最佳答案

我认为问题在于您正在尝试投影类(class)代理对象。投影用于平面数据传输对象。您可以声明此视​​图所需的类(class)属性。

public class PlannerTask
{
public int TaskId { get; set; }
public string TaskName { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
//flatten Course object
public int CourseId { get; set; }
public int CourseName { get; set; }
public bool IsCompleted { get; set; }
}

选择列表可以简化一点......
PlannerTask plannerTask = null;
Course courseAlias = null;

List<PlannerTask> result = session.QueryOver<Task>()
.JoinAlias(x => x.Course, () => courseAlias)
.SelectList(list => list
.Select(x => x.TaskId).WithAlias(() => plannerTask.TaskId)
.Select(x => x.TaskName).WithAlias(() => plannerTask.TaskName)
.Select(x => x.DueDate).WithAlias(() => plannerTask.DueDate)
.Select(x => x.Description).WithAlias(() => plannerTask.Description)
.Select(x => x.Course.Id).WithAlias(() => plannerTask.CourseId))
.Select(x => courseAlias.Name).WithAlias(() => plannerTask.CourseName))
.TransformUsing(Transformers.AliasToBean<PlannerTask>())
.List<PlannerTask>();

关于nhibernate - 如何使用 nhibernate queryover "Select",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5923007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com