views:

163

answers:

1

I have the following class

public class Person
{
    private IList<Person> _children;

    public IEnumerable<Person> Children { get; }

    public void AddChild(Person child)
    {
        // Some business logic and adding to the internal list
    }
}

What changes would I have to make for NHibenrate to be able to persist the Child collection (apart from making everything virtual, I know that one).

Do I have to add a setter to the children property which does something like a _children.Clear(); _children.AddRange(value). Currently the model expresses my intent quite nicely but I'm not sure how much alteration is need for NH to be able to help me out with persistence.

+1  A: 

NHibernate is able to map private fields. Access and naming strategies are discussed in the property section of the reference documentation.

Making your public members virtual is required for proxies to work. These will usually be runtime-generated subclasses of your entity classes.

In this example mapping the field _children will be Children in HQL and Criteria queries.

<class name="Person" table="person">
    <bag name="Children" access="field.camelcase-underscore">
        <key column="parentid" />
        <one-to-many class="Person" />
    </bag>
</class>
Lachlan Roche
awesome, thanks very much. I didn't know it could grab fields too!
Chris Meek