views:

220

answers:

3

I have a database that accepts no null values and has a default for every field. Using fluent nHibernate, I am getting an error on an Insert if I have a component that has some, but not all properties filled out. I am just wondering how to get the DynamicInsert flag down to the component level. Perhaps it is late, but I'll just drop this off here and see where it goes.

mapping:

 public ClientMap()
    {
        Table("Clients");
        DynamicInsert();
        DynamicUpdate();
        CompositeId()
            .KeyProperty(x => x.Accountnumber, "acct_no")
            .KeyProperty(x => x.Transferaccount, "tr_acct_no");

        Component(c => c.Address,
            m =>{
                m.Map(x => x.Streetaddress, "coaddress").Not.Nullable().Default("");
                m.Map(x => x.City, "cocity").Not.Nullable().Default("");
                m.Map(x => x.Postalcode, "costate").Not.Nullable().Default("");
                m.Map(x => x.State, "cozip").Not.Nullable().Default(""); });
    }

test:

Client nw = new Client{ Address = new Address{Streetaddress = "my address"},Accountnumber = "6543219",Transferaccount = "1"};
    IRepository repo2 = new Repository(session);
    repo2.Save(nw);

error:

could not insert: [BusinessObjects.Client#BusinessObjects.Client][SQL: INSERT INTO Clients (coaddress, cocity, cozip, costate, acct_no, tr_acct_no) VALUES (?, ?, ?, ?, ?, ?)]

A: 

NHibernate itself doesn't support dynamic insert on a component mapping - it's not something missing in Fluent.

Suggest you put a default constructor on Address that sets these default property values to empty strings rather than nulls?

David M
Setting the default properties in the constructor then allows me around the insert error. The next problem would be the update strategy when something hasn't changed. The whole component gets updated if one property changes, there must be a standard approach to this as not all consumers of the objects will need all of the data.
Rob A
+3  A: 

I'm not sure, but I think that the 'NotNullable' and 'Default' mapping properties that you have specified, only have influence / are only used when you create a DB schema using your NHibernate mapping, and that they have no real effect on what NHibernate will insert into your DB.

If you want a default value for some property, I think that you should give that property the default value yourself in the constructor of the class that has this property.

Frederik Gheysels
A: 

NHibernate considers a component to be a single well... component :-) That means that it will not do a dynamic insert for components.

Ayende Rahien