views:

57

answers:

1

I am experiencing some odd behaviour with NHibernate. I'm retrieving a list of Learners from a repository, updating them as necessary, the odd thing is when I save the first one, the changes made to all the learners are being commited to the database.

        [Transaction]
        public void UpdateLearner(Learner learner)
        {
            //UnitOfWork.CurrentSession.Save(learner);
        }

Any ideas why? I dont have caching enabled. I know its something to do with the transaction as the changes get persisted even with the call to the save method commented out.

This is my mapping:

<class name="Learner"  table="ILR_Learner">
    <id name="Id" column="ILRLearnerID">
      <generator class="native" />
    </id>
    <property column="LastWarning" name="LastWarning" type="DateTime" />
    <property column="Submitted" name="SuccessfulSubmission" type="DateTime" />

    <join table="vwLearnerLSCUpload">
      <key column="ILRLearnerID" foreign-key="ILRLearnerID"/>
      <property column="Dog" type="DateTime" name="Dog"/>
    </join>

    <join table="Learner">
      <key column="Id" foreign-key="ILRLearnerID"/>
      <property column="Food" name="Food" type="String" length="20" />
    </join>

  </class>
+1  A: 

When updating entities, changes are tracked automatically. So when the transaction is committed all changed entities are persisted. No need to call:

Session.Save(entity);

See this Question.

To disable change tracking per entity you have to evict the entity from the session:

Session.Evict(entity);

To persist any changes, you would then call:

Session.Update(entity);
mxmissile
Interesting. So how do I actually persist just one of my entities without persisting them all?
Dan
I wish there was a way to disable change tracking globally. Ayende had a valid explanation why, but I can't find it ATM.
mxmissile