Thanks for the reply, Gabriel.
I made the change to the collection, adding Cascade.All() to the map. This
did change the generated SQL some, but the test still did not pass. The
detail table's FK is not being set.
WithTable("ZipcodeSet");
Id(c => c.ID)
.WithUnsavedValue(0)
.GeneratedBy.Native();
Map(c => c.Description)
.WithLengthOf(50);
HasMany<ZipcodeSetDetail>(c => c.Details)
.IsInverse()
.WithKeyColumn("ZipCodeSetId")
.Cascade.All()
.AsBag();
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: UPDATE ZipcodeSetDetail SET ZipCode = @p0 WHERE ID = @p1; @p0 =
'90048', @p1 = '1'
NHibernate: INSERT INTO ZipcodeSet (Description) VALUES (@p0); select
last_insert_rowid(); @p0 = 'First two'
NHibernate: SELECT zipcodeset0_.ID as ID1_0_, zipcodeset0_.Description as
Descript2_1_0_ FROM ZipcodeSet zipcodeset0_ WHERE zipcodeset0_....@p0; @p0 =
'1'
NHibernate: SELECT details0_.ZipCodeSetId as ZipCodeS3_1_, details0_.ID as
ID1_, details0_.ID as ID2_0_, details0_.ZipCode as ZipCode2_0_ FROM
ZipcodeSetDetail details0_ WHERE details0_.zipcodeset...@p0; @p0 = '1'
TestCase
'PowRev.Tests.DataIntegration.ZipcodeSetIntegrationTests.CanGetEntityById'
failed:
Expected: 1
But was: 0
From: [email protected]
[mailto:[email protected]] On Behalf Of Gabriel Schenker
Sent: Monday, February 02, 2009 10:39 PM
To: [email protected]
Subject: [fluent-nhib] Re: First attempt at parent/child not working
you forgot the "cascade" for the collection (HasMany)
On Tue, Feb 3, 2009 at 3:56 AM, Lars <[email protected]> wrote:
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
-~----------~----~----~----~------~----~------~--~---