views:

499

answers:

2

Hi there,

I am using for some legacy db the corresponding domainclasses with mappings. Now the Ids of the entities are calculated by some stored Procedure in the DB which gives back the Id for the new row.(Its legacy, I cant change this) Now I create the new entity , set the Id and Call Save. But nothing happens. no exeption. Even NH Profiler does not say a bit. its as the Save call does nothing. I expect that NH thinks that the record is already in the db because its got an Id already. But I am using Id(x => x.id).GeneratedBy.Assigned() and intetionally the Session.Save(object) method.

I am confused. I saw so many samples there it worked.

does any body have any ideas about it?

public class Appendix
{
    public virtual int id { get; set; }
    public virtual AppendixHierarchy AppendixHierachy { get; set; }
    public virtual byte[] appendix { get; set; }
}       

public class AppendixMap : ClassMap<Appendix>
{
    public AppendixMap ()
    {
        WithTable("appendix");
        Id(x => x.id).GeneratedBy.Assigned();
        References(x => x.AppendixHierachy).ColumnName("appendixHierarchyId");
        Map(x => x.appendix);
    }
}
A: 

Are you missing the id column name?

Jon Seigel
+1  A: 

Stupid question, but the reason of this problem in many cases: did you commit the session? NH caches the changes (when calling Save, nothing happens yet) until it is forced to flush it or until you commit the session. When you create your own ADO connection, you need also to call Flush before committing.

Stefan Steinegger
wow Yes that was it! Flush! But then I have to call Flush everytime before I close the session again, dont I? Or do I have to wrap that with an transaction?
urpcor
It depends on your situation. The easiest way is to let NH create the connection, then you never need to call Flush. If you have something special when creating the connection, consider writing a ConnectionProvider, this is not so hard. You should always use transactions, there is support for contextual transactions in NH 2.1, but I don't use it. Normally, the transaction is shorter then the session. Normally you have: sessionFactory.OpenSession, session.BeginTransaction, session.Transaction.Commit, session.Close. But there are other possible scenarios.
Stefan Steinegger
I use in the FluentMapping Id(x=>x.id).GeneratedBy.Assigned(); this is due to the legacy database I have to use. My guess: Since I assign the column Ids by hand NH does not know if there is something immediately to save when I call Save(object). even If I close the session it will not write the dirty newly created object.I dont understand this....
urpcor