如何获取此 sql 查询并将其转换为 nhibernate 查询
我正在尝试将此 sql 查询放入 nhibernate HQL 查询中。我正在使用 nhibernate 3 和 Fluent Nhibernate 1.2
SELECT dbo.Tasks.CourseId, dbo.CoursePermissions.BackgroundColor, dbo.Tasks.DueDate, dbo.Tasks.TaskName, dbo.Tasks.TaskId
FROM dbo.Courses INNER JOIN
dbo.Tasks ON dbo.Courses.CourseId = dbo.Tasks.CourseId INNER JOIN
dbo.CoursePermissions ON dbo.Courses.CourseId = dbo.CoursePermissions.CourseId
WHERE (dbo.Tasks.CourseId = 1)
我本来想使用 linq,但我认为 nhibernate 还不支持 linq join,所以我想我只能使用 HQL(除非有人知道更好的方法)。
我想我可以使用 QueryOver 或 nhibernate 执行查询的其他方式,所以无论哪种方式效果最好。我仍然不明白所有方式之间的区别,就好像我可以在 linq 中做所有事情一样。
但是我不知道如何编写我的查询。
谢谢
编辑
我现在有了这个(稍微改变了)
课程cAlias = null; 任务 tAlias = null; CoursePermission cpAlias = null;
var result = session.QueryOver<Task>(() => tAlias)
.JoinAlias(() => tAlias.Course, () => cAlias)
.JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
.Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
.And(x => x.DueDate >= startDate)
.And(x => x.DueDate <= endDate)
.Select( Projections.Property(() => cAlias.Id),
Projections.Property(() => cpAlias.BackgroundColor),
Projections.Property(() => tAlias.DueDate),
Projections.Property(() => tAlias.TaskName),
Projections.Property(() => tAlias.TaskId))
.List<object[]>();
我知道想要将其映射到
公共类 TaskAppointments { 公共 int Id { 得到;放; } 公共字符串背景颜色{获取;放; } 公共 DateTime DueDate { 获取;放; } 公共 int TaskId { 获取;放; } 公共字符串任务名称{获取;放;我
该
怎么做。如果这是一个 linq 方法,我会这样做
.Select(new TaskAppointments { TaskId = Projections.Property(() => tAlias.TaskId)})
,但它说它无法将其转换为 int。
Edit2
这就是我想出的
Course cAlias = null; 任务 tAlias = null; CoursePermission cpAlias = null; 任务约会 任务约会 = null;
List<TaskAppointments> result = session.QueryOver<Task>(() => tAlias)
.JoinAlias(() => tAlias.Course, () => cAlias)
.JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
.Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
.And(x => x.DueDate >= startDate)
.And(x => x.DueDate <= endDate)
.SelectList(list =>
list.SelectGroup(x => x.TaskId).WithAlias(() => taskAppointments.TaskId)
.SelectGroup(() => cpAlias.BackgroundColor).WithAlias(() => taskAppointments.BackgroundColor)
.SelectGroup(x => x.DueDate).WithAlias(() => taskAppointments.DueDate)
.SelectGroup(x => x.TaskName).WithAlias(() => taskAppointments.TaskName)
)
.TransformUsing(Transformers.AliasToBean<TaskAppointments>())
.List<TaskAppointments>().ToList();
I am trying take this sql query and make it into an nhibernate HQL query. I am using nhibernate 3 and Fluent Nhibernate 1.2
SELECT dbo.Tasks.CourseId, dbo.CoursePermissions.BackgroundColor, dbo.Tasks.DueDate, dbo.Tasks.TaskName, dbo.Tasks.TaskId
FROM dbo.Courses INNER JOIN
dbo.Tasks ON dbo.Courses.CourseId = dbo.Tasks.CourseId INNER JOIN
dbo.CoursePermissions ON dbo.Courses.CourseId = dbo.CoursePermissions.CourseId
WHERE (dbo.Tasks.CourseId = 1)
I would have liked to use linq but I don't think nhibernate supports linq joins yet so I guess I am stuck with using HQL(unless someone knows a better way).
I guess I can use QueryOver or the other ways nhibernate does queries so whatever works the best. I still don't understand the difference between all the ways as if I could do everything in linq I would.
However I have no clue on how to write my query.
Thanks
Edit
I now have this(changed a bit)
Course cAlias = null;
Task tAlias = null;
CoursePermission cpAlias = null;
var result = session.QueryOver<Task>(() => tAlias)
.JoinAlias(() => tAlias.Course, () => cAlias)
.JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
.Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
.And(x => x.DueDate >= startDate)
.And(x => x.DueDate <= endDate)
.Select( Projections.Property(() => cAlias.Id),
Projections.Property(() => cpAlias.BackgroundColor),
Projections.Property(() => tAlias.DueDate),
Projections.Property(() => tAlias.TaskName),
Projections.Property(() => tAlias.TaskId))
.List<object[]>();
I know want to map it to
public class TaskAppointments
{
public int Id { get; set; }
public string BackgroundColor { get; set; }
public DateTime DueDate { get; set; }
public int TaskId { get; set; }
public string TaskName { get; set; }
}
How do I do this. If this was a linq method I would do
.Select(new TaskAppointments { TaskId = Projections.Property(() => tAlias.TaskId)})
but it says it can't convert it to an int.
Edit2
This is what I came up with
Course cAlias = null;
Task tAlias = null;
CoursePermission cpAlias = null;
TaskAppointments taskAppointments = null;
List<TaskAppointments> result = session.QueryOver<Task>(() => tAlias)
.JoinAlias(() => tAlias.Course, () => cAlias)
.JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
.Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
.And(x => x.DueDate >= startDate)
.And(x => x.DueDate <= endDate)
.SelectList(list =>
list.SelectGroup(x => x.TaskId).WithAlias(() => taskAppointments.TaskId)
.SelectGroup(() => cpAlias.BackgroundColor).WithAlias(() => taskAppointments.BackgroundColor)
.SelectGroup(x => x.DueDate).WithAlias(() => taskAppointments.DueDate)
.SelectGroup(x => x.TaskName).WithAlias(() => taskAppointments.TaskName)
)
.TransformUsing(Transformers.AliasToBean<TaskAppointments>())
.List<TaskAppointments>().ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有映射,我假设您具有以下关系:
Courses
->任务
(1:n) 和课程
->课程权限
(1:n)我还假设您不需要完整的对象,而只需要某些属性,因此我使用了投影。
QueryOver版本:
编辑开始
如果需要做WHERE IN子句,可以这样做:
编辑结束
编辑2开始
如果你想将其转换为 DTO:
编辑 2 结束
HQL 版本:
请注意,这将产生笛卡尔积,因此对于每个课程,您将获得 Tasks.Count + CoursePermissions.Count 行。
Without mappings I assume that you have the following relationships:
Courses
->Tasks
(1:n) andCourses
->CoursePermissions
(1:n)I also assumed that you do not want the complete objects but only certain properties, so I used projections.
QueryOver version:
Edit start
If you need to do a WHERE IN clause, you can do this:
Edit end
Edit 2 start
If you want to transform it into a DTO:
Edit 2 end
HQL version:
Be aware that this will result in a cartesian product, so for each Course you will get Tasks.Count + CoursePermissions.Count rows.