Hi there
I'm using NHibernate 1.2.0 GA and I have some strange behavior where a One-to-Many relationship is not correctly persisted if I call more than once SaveOrUpdate with the One end of the relationship.
The tests that fail are is the last assertion, which is :
Assert.IsTrue(testBrand.Programmes[0].Id != 0);
In this case testBrand.Programes[0].Id is 0 and indeed this Programme entity hasn't been persisted into the database.
testProgramme.Id is too 0.
Maybe something I need to note as well is that a Session is kept open per thread when running the unit tests.
On another note, I'd would like to know as well what is the best way to make sure we are testing objects coming from the database, instead of checking against testBrand directly after a SaveOrUpdate, isn't it better to call Session.Refresh(testBrand) to resync this entity with the underlying database ?
From what I gather now it looks like I'm not testing against a persisted entity but only with an object whose Id property has been modified by NHibernate.
Many thanks
Daniel
[Test]
public void CreateBrandSaveAndAddProgrammeToBrand()
{
Brand testBrand = Helper.CreateBrand("TEST BRAND");
using (TransactionScope tx = new TransactionScope())
{
TestHelper.BrandRepository.SaveOrUpdate(testBrand);
tx.Complete();
}
Programme testProgramme = ProgrammeTest.Helper.CreateProgramme("TEST PROGRAMME");
testBrand.AddProgramme(testProgramme);
using (TransactionScope tx = new TransactionScope())
{
TestHelper.BrandRepository.SaveOrUpdate(testBrand);
tx.Complete();
}
Assert.IsTrue(testBrand.Programmes.Count == 1);
Assert.IsTrue(testBrand.Programmes[0].Brand == testBrand);
Assert.IsTrue(testBrand.Programmes[0].Title == "TEST PROGRAMME");
Assert.IsTrue(testBrand.Programmes[0].Series == null);
Assert.IsTrue(testBrand.Series.Count == 0);
Assert.IsTrue(testBrand.Programmes[0].Id != 0);
}
MAPPINGS:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Bbc.Ww.ContentContribution.DomainModel" assembly="Bbc.Ww.ContentContribution.DomainModel">
<!-- TODO: confirm max lengths of fields and non null fields for database schema -->
<class name="Brand" table="Brand">
<!-- IDENTIFIER -->
<id name="_Id" column="brandId" type="Int32" access="field" unsaved-value="0">
<generator class="native" />
</id>
<!-- PROPERTIES -->
<property name="Title" type="String" length="255" not-null="true"/>
<property name="Subtitle" type="String" length="255" not-null="false"/>
<property name="Synopsis" type="String" length="2000" not-null="false"/>
<property name="UseSeriesSequence" type="Boolean" />
<property name="CreatedDate" type="DateTime" not-null="false"/>
<property name="UpdatedDate" type="DateTime" not-null="false"/>
<property name="LockedDate" type="DateTime" not-null="false"/>
<!-- MANY TO ONE RELATIONSHIPS (child to parent) -->
<!-- ONE TO MANY RELATIONSHIPS (parent to children) -->
<bag name="_series" table="Series" access="field" inverse="true" lazy="true" cascade="all">
<key column="brandId"/>
<one-to-many class="Series"/>
</bag>
<bag name="_programmes" table="Programme" access="field" inverse="true" lazy="true" cascade="all">
<key column="brandId"/>
<one-to-many class="Programme"/>
</bag>
</class>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Bbc.Ww.ContentContribution.DomainModel" assembly="Bbc.Ww.ContentContribution.DomainModel">
<!-- TODO: confirm max lengths of fields and non null fields for database schema -->
<class name="Programme" table="Programme">
<!-- IDENTIFIER -->
<id name="_Id" column="programmeId" type="Int32" access="field">
<generator class="native" />
</id>
<!-- PROPERTIES -->
<property name="Title" type="String"/>
<property name="Synopsis" type="String" length="2000" not-null="false"/>
<property name="CreatedDate" type="DateTime" not-null="false"/>
<property name="UpdatedDate" type="DateTime" not-null="false"/>
<property name="LockedDate" type="DateTime" not-null="false"/>
<property name="SequenceNumber" type="Int32" not-null="false"/>
<!-- MANY TO ONE RELATIONSHIPS (child to parent) -->
<many-to-one name="Brand" column="brandId"></many-to-one>
<many-to-one name="Series" column="seriesId"></many-to-one>
<!-- ONE TO MANY RELATIONSHIPS (parent to children) -->
<bag name="_clips" table="ProgrammeClip" access="field" inverse="true" lazy="true" cascade="all">
<key column="programmeId"/>
<one-to-many class="Clip"/>
</bag>
<bag name="_images" table="ProgrammeImage" access="field" inverse="true" lazy="true" cascade="all">
<key column="programmeId"/>
<one-to-many class="Image"/>
</bag>
</class>
|