NHibernate IUserType 未应用于插入
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了以下两个选项:
选项 1:不要使用您自己的 UserType。而是使用 NHibernate 自己的 TimeAsTimeSpan,如下所示:(
示例取自 此处)
选项 2:稍微修改一下你的类:
I found the following two options:
Option 1: do not use your own UserType. Instead use NHibernate's own TimeAsTimeSpan like this:
(Example taken from here)
Option 2: Modify your class a little: