即使所有属性都为null,我如何告诉NHibernate加载一个非null的组件?

Sha*_*bie 4 nhibernate nhibernate-mapping

我有一个包含DateTime的Date类?class(帮助模拟DateTime.Now,我们的域等).

Date类只有一个受保护的属性:DateTime?日期

public class Date
{
    protected DateTime? date;
}

// mapping in hbm
<component name="CompletedOn">
  <property column="StartedOn" name="date" access="field" not-null="false" />
</component>
Run Code Online (Sandbox Code Playgroud)

来自nhibernate文档:

Like all value types, components do not support shared references. The null value semantics of a component are ad hoc. When reloading the containing object, NHibernate will assume that if all component columns are null, then the entire component is null. This should be okay for most purposes.

我可以覆盖此行为吗?即使date为null,我也希望实例化我的Date类.

谢谢,

Jam*_*Ide 5

我认为您可以通过使用实现IPostLoadEventListener的侦听器来实现此行为.加载实体后,您可以检查该组件是否为空,如果是,则检查该组件是否为空.