NHibernate IUserType converts a nullable DateTime to a non-null value

I have an outdated database that stores dates that mean date-9999-21-31, The column Till_Dateis of type DateTime not-null="true".

in the application, I want to build a constant class that represents a date-value as null, So I used a nullable DateTime in C # // public DateTime? TillDate {get; set; }

I created an IUserType that knows to convert an entity null value to DB 9999-12-31

but it seems that NHibernate does not call SafeNullGet, SafeNullSet on my IUserType when the entity value is null and zero is indicated for the null column.

I tried to circumvent it by matching the column as not-null = "false" (only the mapping file was changed, not the database) but it still didn’t help, only now it is trying to insert a null value into the database and get an ADOException.

Any knowledge if NHibernate does not support IUseType that convert null values ​​to non-zero values?

// Run before corrections!

public class NullableDateTimeToNotNullUserType : IUserType
{
        private static readonly DateTime MaxDate = new DateTime(9999, 12, 31);

        public new bool Equals(object x, object y)
        {                                                   //This didn't work as well
            if (ReferenceEquals(x, y)) return true; //if(x == null && y == null) return false;

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

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x == null ? 0 : x.GetHashCode();
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var value = rs.GetDateTime(rs.GetOrdinal(names[0]));

            return (value == MaxDate)? null : value;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var dateValue = (DateTime?)value;

            var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;

            ((IDataParameter)cmd.Parameters[index]).Value = dbValue;
        }

        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 cached;
        }

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

        public SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.DateTime.SqlType }; }
        }

        public Type ReturnedType
        {
            get { return typeof(DateTime?); }
        }

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

// Final implementation with corrections.

//Make the column mapping in hbm.xml not-null="false" even if in DB null not allowed.
//Make sure the class mapping in xml doesn't have dynamic-insert="true"

public class NullableDateTimeToNotNullUserType : IUserType
{
        private static readonly DateTime MaxDate = new DateTime(9999, 12, 31);

        public new bool Equals(object x, object y)
        {                                                   //This didn't work as well
            if (ReferenceEquals(x, y)) return true; //if(x == null && y == null) return false;

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

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x == null ? 0 : x.GetHashCode();
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var value = NHibernateUtil.Date.NullSafeGet(rs, names[0]);

            return (value == MaxDate)? default(DateTime?) : value;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            var dateValue = (DateTime?)value;

            var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;

            NHibernateUtil.Date.NullSafeSet(cmd, valueToSet, index);
        }

        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 cached;
        }

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

        public SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.DateTime.SqlType }; }
        }

        public Type ReturnedType
        {
            get { return typeof(DateTime?); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
    }
}
+3
source share
1 answer

Probably the problem is in your implementation Equals. NHibernate uses Equals to determine if it needs to get or set a value. Please post your implementation of IUserType.

Edit: Well, I think I see the problem. Try the following:

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
    var value = NHibernateUtil.Date.NullSafeGet(rs, names[0]);
    return (value == MaxDate) ? null : value;
}

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
    var dateValue = (DateTime?)value;
    var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;
    NHibernateUtil.Date.NullSafeSet(cmd, valueToSet, index);
}

NullSafeGet DateTime?. Equals .

+1

Source: https://habr.com/ru/post/1738452/


All Articles