【问题标题】:How do I tell NHibernate to load a component as not null even when all its properties are null?我如何告诉 NHibernate 将一个组件加载为非空,即使它的所有属性都为空?
【发布时间】:2010-05-21 06:30:50
【问题描述】:

我有一个包含 DateTime 的 Date 类?类(有助于模拟 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>

来自休眠文档:

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 类。

谢谢,

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    我认为您不能覆盖该行为。

    但是,让 NH 像往常一样工作(检索 null)并将其包装在您的域模型中通常更容易。

    但据我所知,您这样做可能是出于演示(数据绑定?)的考虑。公开DateTime?不是更容易吗?

    【讨论】:

    • 不管怎样,你不能模拟一个静态方法。您应该注入“当前时间”服务。您试图在错误的级别上处理它。
    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 2021-01-24
    • 2010-11-13
    • 2022-09-28
    • 1970-01-01
    • 2012-07-18
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多