我正在尝试理解NHibernate的以下行为,但不能。
session.CreateCriteria<Param>()
.Add(Restrictions.Eq("GroupId", groupId))
.Add(Restrictions.Eq("ParamId", paramId))
.List<Param>()
此构造应返回带有1个数据库行(对象)的集合。但是返回带有单个null元素的列表。你能建议一下吗?
P.S。 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
答案 0 :(得分:0)
那是我的错。 ParamName值不是空字符串。它是空的!所以,这是NHibernate的奇怪行为,但却找到了理由。