NHibernate IUserType 未应用于插入

发布于 2024-11-10 06:40:26 字数 2710 浏览 1 评论 0原文

我使用 NHibernate 和 Postgresql 作为后端,并且必须创建自定义类型来将 System.DateTime 转换为 Postgresql“时间”类型以及将 System.TimeSpans 转换为“间隔”数据库类型。我创建的 IUserType 正在工作,并且正在应用于读取和更新,但当我尝试将对象插入数据库时​​,它们永远不会被应用。我在 IUserTypes 中设置了断点,但在插入时它们永远不会被命中。

我认为问题可能在于该对象只是 POCO 对象而不是代理,因此当我设置属性的值时,不会发生将转换应用于用户类型的映射。

如果我进行更新并设置这些属性之一,我可以看到 IUserType 中的断点被触发。

有什么想法吗?

更新 正在调用 IUserType 的 Equals 方法,但未调用我进行必要转换的 NullSafeSet。

编辑 添加了代码示例

    public class TimeType : BaseImmutableUserType<TimeSpan>
    {
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var val = NHibernateUtil.Time.NullSafeGet(rs, names[0]);
            if (val == null)
                return null;

            var dt = DateTime.Parse(val.ToString());

            return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
        }

        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var obj = (TimeSpan)value;

            ((IDbDataParameter) cmd.Parameters[index]).Value = obj;
        }

        public override SqlType[] SqlTypes
        {
            get
            {
                return new[] { SqlTypeFactory.Time };
            }
        }
    }

    public abstract class BaseImmutableUserType<T> : IUserType
    {
        public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
        public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
        public abstract SqlType[] SqlTypes { get; }

        public BaseImmutableUserType()
        {
            int i = 0;
        }

        public new bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }

            if (x == null || y == null)
            {
                return false;
            }

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object DeepCopy(object value)
        {
            return value;
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public Type ReturnedType
        {
            get { return typeof(T); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
}

I'm using NHibernate with Postgresql as the backend and had to create custom types for converting System.DateTime to Postgresql "time" types as well as System.TimeSpans to "interval" db types. The IUserType I created are working and are being applied for reads and updates but they are never being applied when I try to insert an object into the database. I've set breakpoints in the IUserTypes but they never are hit when an insert is happening.

I think the issue may be around the fact that the object is just a POCO object and isn't a proxy yet so the mapping that applies the transformation to the usertype doesn't happen when I set the value of my property.

If I do an update and set one of these properties I can see the breakpoints in the IUserType are fired.

Any thoughts?

UPDATE
The Equals Method of the IUserType is being called but the NullSafeSet where I do the necessary conversion is not.

EDIT
Added code samples

    public class TimeType : BaseImmutableUserType<TimeSpan>
    {
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var val = NHibernateUtil.Time.NullSafeGet(rs, names[0]);
            if (val == null)
                return null;

            var dt = DateTime.Parse(val.ToString());

            return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
        }

        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var obj = (TimeSpan)value;

            ((IDbDataParameter) cmd.Parameters[index]).Value = obj;
        }

        public override SqlType[] SqlTypes
        {
            get
            {
                return new[] { SqlTypeFactory.Time };
            }
        }
    }

    public abstract class BaseImmutableUserType<T> : IUserType
    {
        public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
        public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
        public abstract SqlType[] SqlTypes { get; }

        public BaseImmutableUserType()
        {
            int i = 0;
        }

        public new bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }

            if (x == null || y == null)
            {
                return false;
            }

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object DeepCopy(object value)
        {
            return value;
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public Type ReturnedType
        {
            get { return typeof(T); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-11-17 06:40:26

我发现了以下两个选项:

选项 1:不要使用您自己的 UserType。而是使用 NHibernate 自己的 TimeAsTimeSpan,如下所示:(

Map(x => x.TimeFrom)
    .CustomType("TimeAsTimeSpan");

示例取自 此处

选项 2:稍微修改一下你的类:

public class TimeType : BaseImmutableUserType<TimeSpan>
{
    // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
    private static readonly DateTime BaseDateValue = new DateTime(1753, 01, 01);

    public override object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var val = NHibernateUtil.TimeAsTimeSpan.NullSafeGet(rs, names[0]);
        if (val == null)
            return null;

        var dt = DateTime.Parse(val.ToString());

        return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
    }

    public override void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        //var obj = (TimeSpan)value;  // we can't use TimeSpan here but need to use DateTime
        // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
        DateTime date = BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
        ((IDbDataParameter)cmd.Parameters[index]).Value = date;
    }

    public override SqlType[] SqlTypes
    {
        get
        {
            return new[] { NHibernate.SqlTypes.SqlTypeFactory.Time };
        }
    }
}

I found the following two options:

Option 1: do not use your own UserType. Instead use NHibernate's own TimeAsTimeSpan like this:

Map(x => x.TimeFrom)
    .CustomType("TimeAsTimeSpan");

(Example taken from here)

Option 2: Modify your class a little:

public class TimeType : BaseImmutableUserType<TimeSpan>
{
    // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
    private static readonly DateTime BaseDateValue = new DateTime(1753, 01, 01);

    public override object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var val = NHibernateUtil.TimeAsTimeSpan.NullSafeGet(rs, names[0]);
        if (val == null)
            return null;

        var dt = DateTime.Parse(val.ToString());

        return new TimeSpan(0, dt.Hour, dt.Minute, dt.Second);
    }

    public override void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        //var obj = (TimeSpan)value;  // we can't use TimeSpan here but need to use DateTime
        // this is taken from the source of NHibernate.Type.TimeAsTimeSpanType
        DateTime date = BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
        ((IDbDataParameter)cmd.Parameters[index]).Value = date;
    }

    public override SqlType[] SqlTypes
    {
        get
        {
            return new[] { NHibernate.SqlTypes.SqlTypeFactory.Time };
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文