views:

202

answers:

1

Components which have no value assigned to any child property will also be set to null when being retrieved from the database.

I don't mind this, it's simply an additional check to see if the component is null or not. However when I try to then update or insert that object into the database with the null component I get the error:

"not-null property references a null or transient [full property name]" which seems rediculous, how come NHibernate will pass me a NULL however it won't accept it itself.

Is there some mapping option I am missing to override this or do i really have to set the component to a value before updating or inserting into the database?

Thanks

+1  A: 

Hello,

If all properties are null the component is null itself when you write, it's a normal behaviour.

When you get from the database if you use an auto-property with a public setter you can have this kind of problem.

if you have this :

public virtual MyComponent MyComponentObject { get; set; }

you have to do this instead :

    private MyComponent _myComponent;
    public virtual MyComponent MyComponentObject 
    {
        get
        {
            if (ReferenceEquals(null, _myComponent))
            {
                _myComponent= new MyComponent();
            }
            return _myComponent;
        }
        set
        {
            _myComponent = value;
        }
    }

HTH,

Kris-I,

Kris-I
Yea I guess I do. Automatic properties really are making me lazy if that didn't even enter my head!Thanks
John_
If I know, it's because I'm lazy too and .... has same problem few weeks ago ;-)
Kris-I
It actually turns out that it was a property on the component which was set to null, however NHibernate whenever it returned the error was complaining about the component rather than one of it's properties. Just mentioned this for any one else that runs across this question.
John_