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

  • SEARCH
  • Home
  • 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 8218299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:44:47+00:00 2026-06-07T12:44:47+00:00

I found this example of a tuplizer to do save 0 when saving null

  • 0

I found this example of a tuplizer to do save 0 when saving null relationships. This is needed since I’m working on a app on a legacy database schema.

I tried the tuplizer here: http://nhforge.org/blogs/nhibernate/archive/2011/01/28/how-to-use-0-instead-of-null-for-foreign-keys.aspx

In that example, I got a nullreferenceexception for the ProxyFactory. Then I found an update to the code here: https://bitbucket.org/jfromaniello/hotgazpachoeg/changeset/87ac41c473ae

However, that doesn’t work for me either. In the last method, SetPropertyValues (described as dirty hack 3, used when reading an object from DB), I get a nullref exception on this part, if(typeof(IEntity), when reading a not related object (not a Sample)

My mapping is as follows (simplified):

   Table("ej_sample");
        Not.LazyLoad();
        Id(s => s.Id, "sampleID").GeneratedBy.Native();
        References<Sample>(s => s.ParentSample, "parentSampleID").NotFound.Ignore();

The parentSampleID column must be 0 when no such object exists.

I figured, I only have to do the dirty hacks on insert and update (possibly in my case only insert).

On insert, I want to create a fake proxy, but the code in [2] loads the entity from the db (possibly to use a Null object?!).

Insert dirty hack:

        public override object[] GetPropertyValuesToInsert(object entity, IDictionary mergeMap, ISessionImplementor session) {
        var values = base.GetPropertyValuesToInsert(entity, mergeMap, session);

        //dirty hack 1
        for(int i = 0; i < values.Length; i++) {
            if(values[i] == null && typeof(IEntity).IsAssignableFrom(getters[i].ReturnType)) {
                values[i] = ((ISession)session).Load(getters[i].ReturnType, 0);
            }
        }
        return values;
    }

I tried creating a fake proxy instead of doing the above:

        public override object[] GetPropertyValuesToInsert(object entity, IDictionary mergeMap, ISessionImplementor session) {
        var values = base.GetPropertyValuesToInsert(entity, mergeMap, session);

        //dirty hack 1
        for(int i = 0; i < values.Length; i++) {
            if(values[i] == null && typeof(IEntity).IsAssignableFrom(getters[i].ReturnType)) {
                //values[i] = ((ISession)session).Load(getters[i].ReturnType, 0);
                values[i] = CreateFakeProxy(i);
            }
        }
        return values;
    }

    private object CreateFakeProxy(int i) {
        object proxy;
        using(var sessionImplementor = _sessionFactory.OpenSession()) {
            proxy = _sessionFactory
                .GetEntityPersister(getters[i].ReturnType.FullName)
                .CreateProxy(0, (ISessionImplementor)sessionImplementor);
        }
        return proxy;
    }

Then I get a nullref exception on the _sessionfactory, which is set in the ctor:

        private readonly ISessionFactoryImplementor _sessionFactory;

    public NullableTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity)
        : base(entityMetamodel, mappedEntity) {
            _sessionFactory = entityMetamodel.SessionFactory;

    }

Any ideas how to accomplish this?

  • 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-06-07T12:44:49+00:00Added an answer on June 7, 2026 at 12:44 pm

    The easy solution is to add the following preinsert and preupdate listener.

    public class NullToZeroEventListener : AuditEventListener, IPreInsertEventListener, IPreUpdateEventListener
    {
        public bool OnPreInsert(PreInsertEvent @event) {
            ZeroNullIds(@event.State, @event.Persister.PropertyNames);
            return false;
        }
    
        public bool OnPreUpdate(PreUpdateEvent @event) {
            ZeroNullIds(@event.State, @event.Persister.PropertyNames);
            return false;
        }
    
        protected internal void ZeroNullIds(Object[] state, string[] propertyNames) {
            for(int i = 0; i < propertyNames.Length; i++) {
                if(state[i] != null || propertyNames[i].EndsWith("ID")) continue;
                state[i] = 0;
            }
        }
    }
    

    In the mapping be sure to ignore 0 ids, for example:
    References<User>(s => s.User, "userID").NotFound.Ignore().LazyLoad();

    In your sessionfactory, add a listener for both preinsert and preupdate events (first shown here):

               .ExposeConfiguration(c =>
                {
                    if(!c.EventListeners.PreInsertEventListeners.Any()) {
                        c.AppendListeners(ListenerType.PreInsert, new IPreInsertEventListener[] { new NullToZeroEventListener() });
                    }
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this example in the App Engine documentation . 1 <% 2 UserService
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
I need to use ExpandableList in my app. I found this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html which
I was working through texture mapping and I found this example which I cant
I'm working on something related to generics in java and I've found this example
I found this example and used it How do you resize a Bitmap under
In the YouTube API docs I found this example : https://gdata.youtube.com/feeds/api/videos?q=surfing&max-results=10&fields=entry[yt:statistics/@viewCount>1000000] Which, of course,
I found this great example of horizontal parallax http://www.perandersen.no/sandbox/parallax/ The only modification I would
Found this is some example CSS I was reading and I'm having a hard
I found this command line search and replace example: find . -type f -print0

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.