我有以下课程:
public class Product
{
public virtual int ID { get; set; }
public virtual string Number { get; set; }
public virtual string Description { get; set; }
public virtual Part Part { get; set; }
public virtual IDictionary<string, string> CustomFields { get; set; }
}
并映射:
<class name="Product" table="PRODUCT">
<id name="ID" column="ID" type="Int32">
<generator class="native" />
</id>
<property name="Number" column="NUM" not-null="true" type="String" />
<property name="Description" column="DESCRIPTION" not-null="false" type="String" />
<map name="CustomFields" lazy="true" generic="true">
<key />
<index column="Name" type="String" />
<element column="Value" type="String" />
<loader query-ref="queryCustomFields" />
</map>
</class>
<sql-query name="queryCustomFields" >
<return-scalar column="Name" type="String" />
<return-scalar column="Value" type="String" />
SELECT ... Name, Value
...
where product.NUM = :Number
</sql-query>
如果我像这样执行命名查询:
IQuery query = session.GetNamedQuery("queryCustomFields");
IList<object> customFields = query.SetString("Number", p.Number).List<object>();
返回有效结果。
但如果我尝试执行此操作:
Product p = session.CreateQuery("select p from Product p where p.Number = '9780060722166'").UniqueResult<Product>();
Console.WriteLine(p.CustomFields.Count);
由于CustomFields为空,它在p.CustomFields.Count上失败。 为什么不填充?