I have a Zone <-> Sensor M;N Relation and they are mapped the following way:
Code:
<class name="Zone" table="zones">
<id name="Num" column="zoneId" type="Int32">
<generator class="assigned"/>
</id>
<property column="zoneName" type="String" name="Name" not-null="true" length="256" />
<property column="enabled" type="Boolean" name="Enabled" not-null="true" />
<bag name="AssignedSensors" table="SensorsInZones" lazy="false" inverse="true" >
<key column="ZoneId"></key>
<many-to-many class="Sensor" column="SensorId"></many-to-many>
</bag>
</class>
Code:
<class name="Sensor" table="sensors">
<id name="Id" column="SensorId" type="Int32">
<generator class="assigned"/>
</id>
<property column="address" type="String" name="Address" not-null="true" length="256" />
<property column="enabled" type="Boolean" name="Enabled" not-null="true" />
<bag name="AssignedZones" table="SensorsInZones" cascade="save-update" >
<key column="SensorId"></key>
<many-to-many class="Zone" column="ZoneId"></many-to-many>
</bag>
</class>
I have a unit test set to create a sensor and then create a zone as part of that sensor's assigned zone.
Here is the Sensor Class's members:
Code:
private string address;
protected PropertyChangedEventHandler _propertyChanged;
private IList<Zone> assignedZones;
private bool enabled;
private int id;
And here is the unit test code:
Code:
Repository repo;
Sensor testSensor = new Sensor();
using (repo = new Repository(RepositoryMode.Updatable))
{
WeatherInfoFetcher weatherQuery = new WeatherInfoFetcher();
testSensor.Id = 1234;
testSensor.Address = "123.45.67.89";
testSensor.Enabled = true;
Zone testZone = new Zone();
testZone.Num = 1;
testZone.Name = "Front Yard";
testZone.Enabled = true;
testSensor.AssignedZones.Add(testZone);
repo.SensorDAO.Insert(testSensor);
}
Here are my errors:
Code:
WeatherInfo.UnitTest.DALFixture.SensorDAOTest:
NHibernate.StaleStateException : Unexpected row count: 0; expected: 1
Code:
NHibernate: INSERT INTO sensors (address, enabled, SensorId) VALUES (@p0, @p1, @p2); @p0 = '123.45.67.89', @p1 = 'True', @p2 = '1234'
NHibernate: UPDATE zones SET zoneName = @p0, enabled = @p1 WHERE zoneId = @p2; @p0 = 'Front Yard', @p1 = 'True', @p2 = '1'
Code:
17:00:33,952 ERROR [TestRunnerThread] AbstractFlushingEventListener [(null)]- Could not synchronize database state with session
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1
at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
at NHibernate.Action.EntityUpdateAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
Any idea what the problem is??