I'm very new to NHibernate and Fluent. I've done OK with mapping a
single object, but my first object containing a collection is not
working. It seems that my mapping is not saving the collection.
The parent table is ZipcodeSet and the child collection is
ZipcodeSetDetail. Can anyone see what I have done wrong?
Thanks, Lars
public class ZipcodeSet
{
private string _description;
private IList<ZipcodeSetDetail> _zipCodeSetDetail;
public ZipcodeSet(string description)
{
_description = description;
initMembers();
}
public ZipcodeSet() {}
public virtual string Description
{
get { return _description; }
set { _description = value; }
}
public virtual IList<ZipcodeSetDetail> Details
{
get { return _zipCodeSetDetail; }
protected set { _zipCodeSetDetail = value; }
}
private void initMembers()
{
_zipCodeSetDetail = new List<ZipcodeSetDetail>();
}
}
public class ZipcodeSetDetail
{
private string _zipCode;
public ZipcodeSetDetail(string zipCode)
{
_zipCode = zipCode;
}
public ZipcodeSetDetail() {}
public virtual string ZipCode
{
get { return _zipCode; }
set { _zipCode = value; }
}
}
Mapping:
WithTable("ZipcodeSet");
Id(c => c.ID)
.WithUnsavedValue(0)
.GeneratedBy.Native();
Map(c => c.Description)
.WithLengthOf(50);
HasMany<ZipcodeSetDetail>(c => c.Details)
.IsInverse()
.WithKeyColumn("ZipCodeSetId")
.AsBag();
WithTable("ZipcodeSetDetail");
Id(c => c.ID)
.WithUnsavedValue(0)
.GeneratedBy.Native();
Map(c => c.ZipCode)
.WithLengthOf(5);
Testing:
protected override void LoadTestData()
{
ZipcodeSetDetail zipcodeSetDetail1 = saveDetail(new
ZipcodeSetDetail("90048"));
ZipcodeSetDetail zipcodeSetDetail2 = saveDetail(new
ZipcodeSetDetail("90049"));
ZipcodeSet set1 = new ZipcodeSet("First set");
set1.Details.Add(zipcodeSetDetail1);
saveEntity(set1);
ZipcodeSet set2 = new ZipcodeSet("First two");
set1.Details.Add(zipcodeSetDetail1);
set1.Details.Add(zipcodeSetDetail2);
saveEntity(set2);
}
private void saveEntity(ZipcodeSet set)
{
_repository.SaveOrUpdate(set);
FlushSessionAndEvict(set);
}
private ZipcodeSetDetail saveDetail(ZipcodeSetDetail detail)
{
_repositoryDetail.SaveOrUpdate(detail);
FlushSessionAndEvict(detail);
return detail;
}
public void CanGetEntityById()
{
ZipcodeSet set = _repository.Get(1);
Assert.That(set.Description, Is.EqualTo("First set"));
Assert.That(set.Details.Count, Is.EqualTo(1));
}
SQL results:
NHibernate: INSERT INTO ZipcodeSetDetail (ZipCode) VALUES (@p0);
select last_insert_rowid(); @p0 = '90048'
NHibernate: INSERT INTO ZipcodeSetDetail (ZipCode) VALUES (@p0);
select last_insert_rowid(); @p0 = '90049'
NHibernate: INSERT INTO ZipcodeSet (Description) VALUES (@p0); select
last_insert_rowid(); @p0 = 'First set'
NHibernate: INSERT INTO ZipcodeSet (Description) VALUES (@p0); select
last_insert_rowid(); @p0 = 'First two'
NHibernate: SELECT zipcodeset0_.ID as ID2_0_, zipcodeset0_.Description
as Descript2_2_0_ FROM ZipcodeSet zipcodeset0_ WHERE
zipcodeset0_....@p0; @p0 = '1'
NHibernate: SELECT detail0_.ZipCodeSetId as ZipCodeS3_1_, detail0_.ID
as ID1_, detail0_.ID as ID3_0_, detail0_.ZipCode as ZipCode3_0_ FROM
ZipcodeSetDetail detail0_ WHERE detail0_.zipcodeset...@p0; @p0 = '1'
TestCase
'PowRev.Tests.DataIntegration.ZipcodeSetIntegrationTests.CanGetEntityById'
failed:
Expected: 1
But was: 0
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---