0

我试图理解 NHibernate 的以下行为并且不能。

session.CreateCriteria<Param>()
  .Add(Restrictions.Eq("GroupId", groupId))
  .Add(Restrictions.Eq("ParamId", paramId))
  .List<Param>()

此构造应返回具有 1 个数据库行(对象)的集合。但返回具有单个空元素的列表。你能建议为什么吗?

PS Param 类映射有一个复合键。

public class Param
{
    public Param()
    {
    }
    public Param(string groupId, string paramId, string paramName) : this()
    {
        GroupId = groupId;
        ParamId = paramId;
        ParamName = paramName;
    }
    public virtual string GroupId { get; protected set; }
    public virtual string ParamId { get; protected set; }
    public virtual string ParamName { get; protected set; }
    public virtual string Value { get; set; }

    public override bool Equals(object obj){...}
    public override int GetHashCode(){...}
}

public class ParamMap : ClassMap<Param>
{
    public ParamMap()
    {
        Table("Params");

        CompositeId()
            .KeyProperty(x => x.GroupId)
            .KeyProperty(x => x.ParamId)
            .KeyProperty(x => x.ParamName, "ParamName");

        Map(x => x.Value, "ParamValue");
    }
}

此查询是从数据库日志中复制的。NHibernate 执行它。

SELECT THIS_.GROUPID AS GROUPID19_0_, THIS_.PARAMID AS PARAMID19_0_,
THIS_.PARAMNAME AS PARAMNAME19_0_, THIS_.PARAMVALUE AS PARAMVALUE19_0_
FROM PARAMS THIS_ WHERE THIS_.GROUPID = :1 AND THIS_.PARAMID = :2 
\
WorkHour,TotalHours, 
/

返回 1 行:

GroupId = "WorkHour"
ParamId = "TotalHours"
ParamName = (empty) 
ParamValue = YES
4

1 回答 1

0

那是我的错。ParamName 值不是空字符串。是空的!所以,这是 NHibernate 的奇怪行为,但找到了原因。

于 2013-05-21T10:45:02.473 回答