将类型为date的命名参数插入HQL Insert

时间:2012-04-23 21:15:10

标签: c# nhibernate hql

我正在尝试执行HQL插入语句,将一些参数插入到已映射到实体的表中。

以下是我目前正在进行的工作(抛出异常):

Insert Into Entity1
(ForeignKey1, ForeignKey2, Date1, Date2)

SELECT this_.ForeignKey1, 
       cast(:param1 as int) as ForeignKeyValue2, 
       cast(:param2 as DateTime) as DateValue1, 
       cast(:param3 as DateTime) as DateValue2
FROM OtherEntity this_
WHERE ...

如果我省略了2个日期字段,插件就可以了,所以我知道我很接近。我只需要弄清楚如何让nhibernate看到我的日期作为日期。

例外:

insertion type [NHibernate.Type.Int32Type] and selection type  
[NHibernate.Dialect.Function.CastFunction+LazyType] at position 1 are not compatible
[Insert Into Entity1
(ForeignKey1, ForeignKey2, Date1, Date2)

SELECT this_.ForeignKey1, 
       cast(:param1 as int) as ForeignKeyValue2, 
       cast(:param2 as DateTime) as DateValue1, 
       cast(:param3 as DateTime) as DateValue2
FROM OtherEntity this_
WHERE ...

如果有人有这方面的经验,请随时提供帮助。此外,如果您知道如何在DateTimes上使用HQL强制转换也会有所帮助。 DBMS是MS SQL 2008。:

1 个答案:

答案 0 :(得分:2)

您永远不需要施放,只需将参数设置为正确的类型,例如

var hql = "insert into Entity1 (fkid, mydate) 
  select fkid, :date from OtherEntity";

session.CreateQuery(hql)
  .setDateTime("date", DateTime.Now)
  .ExecuteUpdate();