Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3405330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:29:33+00:00 2026-05-18T05:29:33+00:00

I have a legacy schema that contains tables with primary keys of type binary(16)

  • 0

I have a legacy schema that contains tables with primary keys of type binary(16) — its a MD5 hash of the other columns. NHibernate does not work with byte[] as a key since it does not implement Equals so I wrapped this in a custom type and provided NHibernate with an implementation of IUserType. Notice that MD5Hash is a struct and not a class.

public struct MD5Hash : IComparable, IComparable<MD5Hash>, IEquatable<MD5Hash> {
    private readonly byte[] contents;
    ...
}

Everything worked fine until I created a many-to-one mapping to a type that uses MD5Hash as its key.

public class Referenced : IEquatable<Referenced> {
    ...
    public virtual MD5Hash Id { get; set; }
    public virtual string Name { get; set; } // must NOT be null
    ...
}

public class Referencer : IEquatable<Referencer> {
    ...
    public virtual MD5Hash Id { get; set; }
    public virtual Referenced Other { get; set } // may be null
    ...
}

When I attempt to load objects of type Referencer, NHibernate does not see a null value for the key when the row contains a NULL value so it attempts to instantiate an object of type
Referenced, assign it to Referencer, and update Referencer in the database. Since Referenced has a property, Name, which maps to a non-nullable column, NHibernate raises an exception. What I want is for NHibernate to set the Other property to null.

I could change the definition of MD5Hash to be a class instead of a struct but I have an unknown number of places in the code that probably assumes MD5Hash can never be null so I am looking for another solution.

The code for the custom type…

internal class MD5HashType : IUserType {
    public SqlType[] SqlTypes {
        get { return new[] { new SqlType(DbType.Binary, 16) }; }
    }

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

    public new bool Equals(object x, object y) {
        return Object.Equals(x, y);
    }

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

    public object NullSafeGet(IDataReader rs, string[] names, object owner) {
        var val = NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
        return (null == val || DBNull.Value == val) ? MD5Hash.Empty : new MD5Hash((byte[])val);
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index) {
        var val = (MD5Hash.Empty == ((MD5Hash)value)) ? null : ((MD5Hash)value).ToByteArray();
        NHibernateUtil.Binary.NullSafeSet(cmd, val, index);
    }

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

    public bool IsMutable {
        get { return false; }
    }

    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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:29:33+00:00Added an answer on May 18, 2026 at 5:29 am

    The problem appears to be that NHibernate can’t tell that MD5Hash.Empty means no value. Have you tried creating custom event listeners such as the following to handle this? Something like:

    public class CustomLoadListener : DefaultLoadEventListener {
        public override void OnLoad(LoadEvent @event, LoadType loadType) {
            if(@event.EntityId is MD5Hash) {
                var id = (MD5Hash) @event.EntityId;
                if(id == MD5Hash.Empty) {
                    @event.Result = new Referenced { Id = MD5Hash.Empty };
                    return;
                }
            }
            base.OnLoad(@event, loadType);
        }
    }
    
    public class CustomSaveOrUpdateListener : DefaultSaveOrUpdateEventListener {
        public override void OnSaveOrUpdate(SaveOrUpdateEvent @event) {
            var entity = @event.Entity as Referenced;
            if(entity != null && entity.Id == MD5Hash.Empty) {
                return;
            }
            base.OnSaveOrUpdate(@event);
        }
    }
    

    You would then have to configure these listeners in your session factory via hibernate.cfg.xml:

    <session-factory>
        <!-- various properties -->
        <listener type="load" class="NhHacking.CustomLoadListener, NhHacking"/>
        <listener type="save-update" class="NhHacking.CustomSaveOrUpdateListener, NhHacking"/>
    </session-factory>
    

    If someone has a better idea of how to accomplish this, I would love to hear it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy intranet web app that was written for IE7 and contains
I have an applicaton that contains some tables that are auto-generated from Grails domain
I have two tables in a legacy database that I need to map using
I have a 'legacy' DB2 database that has many other applications and users. Trying
I have legacy DB that store dates that means no-date as 9999-21-31, The column
I have a legacy stored procedure that I'm trying to modify. This is a
We have a legacy WinCE device that's been working fine for years when it
We have a legacy system that requires a format which is not html but
I want to access a legacy database schema from Rails. I have one table
I have a Java-application that loads data from a legacy file format into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.