我正在尝试INSERT INTO ... SELECT ...
用 HQL 编写查询。但是,我插入的类包含<component>
映射中的项目。例如,我的类定义可能是:
public class X
{
public virtual string A { get; set; }
public virtual string B { get; set; }
public virtual Y C { get; set; }
}
public class Y
{
public virtual string C1 { get; set; }
public virtual string C2 { get; set; }
}
和一个可能的映射:
<class name="X">
<id type="int"><generator class="identity"/></id>
<property name="A" />
<property name="B" />
<component name="C">
<property name="C1" column="C1" />
<property name="C2" column="C2" />
</component>
</component>
</class>
如何在 HQL 中为此类编写插入查询?我试过:
session.CreateQuery("INSERT INTO X(A, B, C1, C2) SELECT A, B, 'foo', 'bar' FROM X data")
.ExecuteUpdate();
这给出了错误:
NHibernate.QueryException : could not resolve property: C1 of: X
[INSERT INTO X(A, B, C1, C2) SELECT A, B, "foo", "bar" FROM X data]
我也试过:
session.CreateQuery("INSERT INTO X(A, B, C) SELECT A, B, :C FROM X data")
.SetParameter("C", new Y() { C1 = "foo", C2 = "bar" });
.ExecuteUpdate();
这给出了错误:
NHibernate.HibernateException : Could not determine a type for class: Y
我也试过:
session.CreateQuery("INSERT INTO X(A, B, C.C1, C.C2) SELECT A, B, 'foo', 'bar' FROM X data")
.ExecuteUpdate();
这给出了错误:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown. near line 1, column 21
[INSERT INTO X(A, B, C.C1, C.C2) SELECT A, B, "foo", "bar" FROM X data]
还有其他建议吗?