C# 用相关子查询替换HQL的NHibernate fluent(QueryOver)
背景,使用FluentNHibernate,使用NHibernate 3.0的最新开发版本 以下是WorkIncident的类型声明:C# 用相关子查询替换HQL的NHibernate fluent(QueryOver),c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,背景,使用FluentNHibernate,使用NHibernate 3.0的最新开发版本 以下是WorkIncident的类型声明: // Enumeration used in class below. public enum TicketStatus { Open = 1, Closed = 10, Hold = 20 } // Ticket class. public class WorkIncident { public virtual int Ent
// Enumeration used in class below.
public enum TicketStatus
{
Open = 1,
Closed = 10,
Hold = 20
}
// Ticket class.
public class WorkIncident
{
public virtual int EntryId { get; set; }
public virtual int TicketNumber { get; set; }
public virtual string ModifierNtId { get; set; }
public virtual DateTime ModifiedDate { get; set; }
public virtual TicketStatus Status { get; set; }
public virtual int Version { get; set; }
public virtual string Title { get; set; }
public virtual string Details { get; set; }
}
// FluentNHibernate mapping
public class WorkIncidentMap : ClassMap<WorkIncident>
{
public WorkIncidentMap()
{
Table("incident_details");
Id( wi => wi.EntryId, "wiid");
Map(wi => wi.TicketNumber, "workitem_number");
Map(wi => wi.Title, "workitem_title");
Map(wi => wi.Details, "workitem_comment");
Map(wi => wi.ModifiedDate, "workitem_modified_on");
Map(wi => wi.ModifierNtId, "modified_by_worker_nt_id");
Map(wi => wi.Status, "workitem_status_lookup_id").CustomType<EnumType<Status>>();
Map(wi => wi.Version, "workitem_version");
}
}
此查询为我提供了由工作人员置于关闭状态的所有工作项的列表。考虑到票证存储在数据库中的方式,每个票证都有多条记录,每次更新一条记录,主管通常会在工作人员关闭票证后向票证添加注释,这导致我无法仅查看处于关闭状态的上一版本号来可靠地告诉我是谁关闭了票证
任何帮助都将不胜感激,因为我更愿意尽可能远离HQL和魔术字符串。我认为这应该可以解决问题。最难的部分是真正地处理你的数学运算。您必须进入SQLFunction投影
session.QueryOver<WorkIncident>(() => t1Alias)
.Where(w => w.ModifierNtId == "test")
.And(w => w.ModifiedDate < DateTime.Now && w.ModifiedDate > DateTime.Now)
.And(w => w.Status == TicketStatus.Open)
.And(Restrictions.Disjunction()
.Add(Restrictions.Where<WorkIncident>(w => w.TicketNumber == 1))
.Add(Subqueries.WhereProperty(() => t1Alias.TicketNumber).In(
QueryOver.Of<WorkIncident>(() => t2Alias)
.Where(() => t2Alias.Status != t1Alias.Status)
.And(() => t2Alias.TicketNumber == t1Alias.TicketNumber)
.And(Restrictions.EqProperty(
Projections.Property<WorkIncident>(w=> w.Version),
Projections.SqlFunction(
new VarArgsSQLFunction("(","-",")"),
NHibernateUtil.Int32,
Projections.Property(()=> t1Alias.Version),
Projections.Constant(1)
)))
.Select(w => w.TicketNumber)))
).List();
在我的测试中,这生成了以下SQL
SELECT <snip...>
FROM incident_details this_
WHERE this_.modified_by_worker_nt_id = @p0
and (this_.workitem_modified_on < @p1 and this_.workitem_modified_on > @p2)
and this_.workitem_status_lookup_id = @p3
and (this_.workitem_number = @p4
or this_.workitem_number in
(SELECT this_0_.workitem_number as y0_
FROM incident_details this_0_
WHERE not (this_0_.workitem_status_lookup_id = this_.workitem_status_lookup_id)
and this_0_.workitem_number = this_.workitem_number
and this_0_.workitem_version = (this_.workitem_version-@p5)));
@p0 = 'test' [Type: String (0)], @p1 = 10/26/2012 11:26:24 PM [Type: DateTime (0)], @p2 = 10/26/2012 11:26:24 PM [Type: DateTime (0)], @p3 = 1 [Type: Int32 (0)], @p4 = 1 [Type: Int32 (0)], @p5 = 1 [Type: Int32 (0)]
session.QueryOver<WorkIncident>(() => t1Alias)
.Where(w => w.ModifierNtId == "test")
.And(w => w.ModifiedDate < DateTime.Now && w.ModifiedDate > DateTime.Now)
.And(w => w.Status == TicketStatus.Open)
.And(Restrictions.Disjunction()
.Add(Restrictions.Where<WorkIncident>(w => w.TicketNumber == 1))
.Add(Subqueries.WhereProperty(() => t1Alias.TicketNumber).In(
QueryOver.Of<WorkIncident>(() => t2Alias)
.Where(() => t2Alias.Status != t1Alias.Status)
.And(() => t2Alias.TicketNumber == t1Alias.TicketNumber)
.And(Restrictions.EqProperty(
Projections.Property<WorkIncident>(w=> w.Version),
Projections.SqlFunction(
new VarArgsSQLFunction("(","-",")"),
NHibernateUtil.Int32,
Projections.Property(()=> t1Alias.Version),
Projections.Constant(1)
)))
.Select(w => w.TicketNumber)))
).List();
SELECT <snip...>
FROM incident_details this_
WHERE this_.modified_by_worker_nt_id = @p0
and (this_.workitem_modified_on < @p1 and this_.workitem_modified_on > @p2)
and this_.workitem_status_lookup_id = @p3
and (this_.workitem_number = @p4
or this_.workitem_number in
(SELECT this_0_.workitem_number as y0_
FROM incident_details this_0_
WHERE not (this_0_.workitem_status_lookup_id = this_.workitem_status_lookup_id)
and this_0_.workitem_number = this_.workitem_number
and this_0_.workitem_version = (this_.workitem_version-@p5)));
@p0 = 'test' [Type: String (0)], @p1 = 10/26/2012 11:26:24 PM [Type: DateTime (0)], @p2 = 10/26/2012 11:26:24 PM [Type: DateTime (0)], @p3 = 1 [Type: Int32 (0)], @p4 = 1 [Type: Int32 (0)], @p5 = 1 [Type: Int32 (0)]