Gro*_*roo 3 c# nhibernate hql where-in
我在编写使用"where in"子句的HQL查询时遇到问题.
简化的类看起来像这样:
class Parent
{
public virtual Int64 Id { get; private set; }
public virtual string Name { get; private set; }
}
class Child
{
public virtual Int64 Id { get; private set; }
public virtual string Name { get; private set; }
public virtual Parent Parent { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
使用如下定义的映射:
class ParentMap : ClassMap<Parent>
{
Id(x => x.Id);
Map(x => x.Name);
}
class ChildMap : ClassMap<Child>
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Parent);
}
Run Code Online (Sandbox Code Playgroud)
我想获取Child属于某些Parent项目的实例,因此我编写了以下代码:
// get children for several parents (a flattened list)
IEnumerable<Parent> parents = GetParents();
// use hql
IQuery q = Session.CreateQuery("from Child as c where c.Parent in (:parents)");
q.SetParameter("parents", parents);
Run Code Online (Sandbox Code Playgroud)
但问题是,我得到以下例外q.SetParameter:
Test method Can_get_children_for_many_parents threw exception: NHibernate.PropertyAccessException: Exception occurred getter of Some.Namespace.Parent.Id ---> System.Reflection.TargetException: Object does not match target type.
[编辑]
我尝试使用q.SetParameter("parents", parents.Select(p => p.Id);,但我得到了同样的例外.
| 归档时间: |
|
| 查看次数: |
1528 次 |
| 最近记录: |