0
我想了解NHibernate的以下行爲,不能。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
你能告訴我們更多關於Param類和數據庫中的值嗎? – JTMon
我更新了問題。 – Adelf
嘗試不使用關鍵屬性上的_protected_修飾符?另外,您的Equals和GetHashCode是否正確處理paramName的空值? – jbl