Hibernate version: 1.0.2.0
Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="TranslateHelper.Domain.Classes" assembly="TranslateHelper.Domain" default-access="field.camelcase">
<class name="Area" table="Area">
<jcs-cache usage="read-write" />
<id name="Id" column="AreaId" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" not-null="true" />
<property name="Description" column="Description" type="String" not-null="true" />
<property name="Number" column="Number" type="int" not-null="true" />
<property name="Date" column="Date" type="DateTime" not-null="true" />
<property name="CheckBox" column="CheckBox" type="boolean" not-null="true" />
<bag name="Elements" table="Element" lazy="true" cascade="none">
<key column="AreaId" />
<many-to-many class="Element" column="ElementId" />
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public Area CreateArea()
{
Area a = new Area();
a.Name = "test";
a.Description = "test";
a.Number = 1;
a.Date = new DateTime(1900, 1, 1);
a.CheckBox = false;
Repository.Instance.ActiveSession.SaveOrUpdateCopy(a);
Repository.Instance.ActiveSession.Flush();
Repository.Instance.ActiveSession.Evict(a);
return a;
}
public static void Compare(Area expected, Area candidate)
{
Assert.AreEqual(expected.Id, candidate.Id, "Id differs");
Assert.AreEqual(expected.Name, candidate.Name, "Name differs");
Assert.AreEqual(expected.Number, candidate.Number, "Number differs");
Assert.AreEqual(expected.Description, candidate.Description, "Description differs");
Assert.AreEqual(expected.CheckBox, candidate.CheckBox, "CheckBox differs");
Assert.AreEqual(expected.Date, candidate.Date, "Date differs");
//Assert.AreSame(expected.Elements, candidate.Elements, "Elements differs");
}
#endregion
#region Tests
[Test]
public void Create()
{
Area a = CreateArea();
Assert.IsNotNull(a);
Assert.AreEqual(1, a.Id);
}
[Test]
public void LoadById()
{
//create an Area
Area a = CreateArea();
// Go get another copy of it
Area b = Repository.AreaById(a.Id);
Compare(a, b);
}
[Test]
public void LoadAll()
{
//should be empty initially
IList all = Repository.GetAll("Area");
Assert.AreEqual(all.Count, 0);
//create a couple of Areas to test
Area a = CreateArea();
Area b = CreateArea();
all = Repository.GetAll("Area");
Assert.AreEqual(all.Count, 2);
}
[Test]
public void BulkTest()
{
//should be empty initially
IList all = Repository.GetAll("Area");
Assert.AreEqual(all.Count, 0);
//create Area objects in bulk, and test that the ids are correctly created
int iStartId = -1;
for (int x = 0; x < 1000; x++)
{
Area a = CreateArea();
if (iStartId == -1)
iStartId = a.Id;
Assert.AreEqual(a.Id, iStartId + x, String.Format("Id's differed on loop {0}", x));
}
}
#endregion
}
Full stack trace of any exception that occurs:
TranslateHelper.Domain.Test.AreaFixture.LoadById : NHibernate.NonUniqueObjectException : a different object with the same identifier value was already associated with the session: 1, of class: TranslateHelper.Domain.Classes.Area
Name and version of the database you are using:
SQL Server 2000
I am writing some unit tests for my web app. I have included the code of the unit tests above. I have an onsetup method for my tests, so between each one, I truncate the DB table containing the Area objects, and re-seed it so that the identity starts from 1 again.
The tests run in the following order:
BulkTest()
Create()
LoadAll()
LoadById()
The first 3 tests all run fine. The 4th test gives me the NonUniqueObjectException error. I could understand this if I got the error on the 2nd and 3rd tests, since the Area object of ID 1 is being created in all of the tests, but the first 3 tests fine. The error only occurs on the 4th test. I have tried every method of clearing the session I can find, and also using the SaveOrUpdateCopy() call as reccommended by the docs, but nothing helps.
If anyone could help explain this behaviour to me I would be most grateful!
Regards, Ben