1

我正在尝试执行 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。:

4

1 回答 1

2

您永远不需要强制转换,只需将参数设置为正确的类型,例如

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

session.CreateQuery(hql)
  .setDateTime("date", DateTime.Now)
  .ExecuteUpdate();
于 2012-04-24T07:58:06.597 回答