views:

33

answers:

1

Very new to NHibernate so I expect there will be a quick fix here.

The code I'm looking at has an unorthodox design.

public class Partner
{
    public virtual int Id { get; set;} //note the set is not private
    public virtual String Name { get; set;}
}

and is mapped with FluentNhibernate's Automappings

When Partner is created its Id is 0 but when I call Flush to persist to the db:

        CurrentSession.SaveOrUpdate(obj);
        CurrentSession.Flush();

I get a Unexpected row count: 0; expected: 1 StaleStateException

I'm assuming its because NHibernate doesn't like me changing the Primary Key, Id. In this case I need to. How can I configure NH to let me achieve this abomination?

+1  A: 

You can do the update with HQL:

session.CreateQuery("update Partner set Id = :newId where Id = :oldId")
       .SetParameter("newId", newId)
       .SetParameter("oldId", oldId)
       .ExecuteUpdate();

I suggest that you Evict the entity from the session, or use a stateless one if you are going to update the Id.

Diego Mijelshon