- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
背景,使用 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 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");
}
}
映射工作正常,我可以毫无问题地进行如下查询:
session.QueryOver<AltirisIncident>()
.Where(ai => ai.ModifierNtId == worker.Name.Replace("\\", @"\"))
.AndRestrictionOn(ai => ai.ModifiedDate)
.IsBetween(DateTime.Today)
.And(DateTime.Today.AddDays(1))
.List<WorkIncident>();
这为我提供了特定用户在当前日期触及的所有工作项(基本上是帮助台故障单)。
但是,我一直无法将以下 HQL 翻译成流畅的声明:
from WorkIncident as t1
where t1.ModifierNtId = :ntid
and t1.ModifiedDate between :startdate and :enddate
and t1.Status = :status
and (t1.Version = 1
or t1.TicketNumber in (
select t2.TicketNumber
from WorkIncident as t2
where t2.Status != t1.Status
and t2.TicketNumber = t1.TicketNumber
and t2.Version = t1.Version - 1))
此查询为我提供了由工作人员置于关闭状态的所有工作项的列表。考虑到工单在数据库中的存储方式(每张工单有多个记录(每次更新一个记录)并且主管通常会在工作人员关闭工单后向工单添加注释,导致我无法只查看工单的情况具有关闭状态的最后版本号,可以可靠地告诉我谁关闭了工单。
任何帮助将不胜感激,因为我更愿意尽可能地远离 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)]
关于c# - NHibernate fluent (QueryOver) 用相关子查询替换 HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5384144/
我一直在为初学者阅读 nhibernate 3.0 并阅读了一些常见错误(我犯了一些错误) 我想知道将一个或多个记录设为只读有哪些策略。现在我取回所有行并循环遍历它们,使它们通过 session.Re
我们有一个使用 NHibernate 的相当健壮的系统,我们正在从单个数据库服务器迁移到拥有两台服务器,一台用于管理,另一台用于我们面向公众的网站。这主要是为了让我们可以使用不会影响当前站点的工作流来
我在尝试构建一个时遇到以下错误 session 工厂: PersistenceTests.Can_Map_Orders_To_Database : Failed System.IndexOutOfRa
如果客户有很多订单附加到他们。您将如何使用 NHibernate 延迟加载订单列表。 是不是需要设置映射文件?任何帮助或示例都会很棒。 最佳答案 Chris 的建议是我会怎么做,但是如果您想在运行时执
我正在尝试使用 HQL 对一个简单的查询进行分页,并将总行数作为同一查询的一部分进行检索。 我的查询很简单... var members = UnitOfWork.CurrentSession.Cre
我有旧版数据库,存储的日期表示无日期为9999-21-31, 列Till_Date的类型为DateTime not-null="true"。 在应用程序中,我要构建持久化类,将no-date表示为nu
您可以指定命名空间和程序集以使用 HBM 文件顶部的类型: 您可以在同一个映射文件中使用来自多个程序集/命名空间的类型,如果可以,这样做的语法是什么? 最佳答案 您可以从 HBM 文件的顶部删除默认
如何强制 NHibernate 在多对多集合上执行 RIGHT 外连接或 INNER 连接而不是 LEFT 外连接? 我想这样做的原因是因为过滤应用于集合元素。使用左连接,您将获得与未过滤查询相同的返
我们开始在我的工作场所使用NHibernate,包括从映射生成模式。我们的DBA想要的一件事是主键和外键关系的名称一致。我已经能够设置FK约束名称,但是在的文档中看,它似乎不存在命名主键约束的方法。
我需要NHibernate来执行这样的查询: SELECT * FROM Users ORDER BY Func(FirstName, LastName) Standart NHibernate Or
假设在一个实体中有属性 id、用户名、年龄、地址。现在我只想要 id 和 username 并使用此代码。 投影可以从查询中返回实体列表以外的内容。 var proj = Projections.Pr
我花了很长时间,但我终于让 nHibernate 的 Hello World 工作了。在我做了“延迟加载”之后它起作用了。老实说,我无法告诉您为什么这一切都有效,但确实如此,现在我正在阅读您不需要延迟
假设您有两个类,Person 和 Address。 Person 有一个对 Address 的引用,如下所示: public class Person { public virtual Addre
我在 NHibernate 引用文档 中阅读第 10 章“只读实体”如下: http://nhibernate.info/doc/nh/en/index.html#readonly 但不幸的是我不知道
有谁知道 NHibernate 是否支持从存储过程返回输出参数?我在文档中进行了搜索,但无法真正找到任何可以确认的内容。 最佳答案 我面临同样的问题。 NHibernate 不允许您以这种方式使用存储
简而言之,什么工作得更快: SessionFactory 预编译 XML 配置,或 流畅的NHibernate提供 以编程方式配置? 最佳答案 我个人的经验是,Configuration 对象的构建(
我的域类具有如下所示的集合: private List _foos = new List(); public virtual ReadOnlyCollection Foos { get { retur
当我有一个带有一对多子集合的实体对象时,我需要查询一个特定的子对象,是否有我还没有想出的功能或一些巧妙的模式来避免 NHibernate获取整个子集合? 例子: class Parent {
在我的域中,员工和部门具有一对多的双向关系;为了让子员工同步这个,我有一个“内部”访问字段,用于部门中员工的集合(NHibernate 的 Iesi),否则它将是只读公共(public)的。像这样:
我有一个 nhibernate 自定义类型,我想用 Fluent NHibernate 映射它。 HBM 映射如下所示。 Services.Data.DateConventionTyp
我是一名优秀的程序员,十分优秀!