Hello people,
sparing you the details of my lifeline so far, let me confront you
with what is having me scratching my head.
I cannot get my unidirectional association of Customer->Addresses to
save. Since I cannot attach a small solution here the very simple
setup in text:
Customer:
public class Customer
{
public Customer()
{
addresses = new List<Address>();
}
private IList<Address> addresses;
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Customer AddAddress(Address adr)
{
addresses.Add(adr);
return this;
}
public virtual IList<Address> Addresses
{
get { return addresses; }
}
}
Address:
public class Address
{
protected Guid id;
}
Mappings:
<class name="Customer" table="Customer" >
<id name="Id" column="ID">
<generator class="guid" />
</id>
<property name="Name" column="Name" not-null="true" />
<list name="Addresses" access="nosetter.camelcase" cascade="all">
<key column="CustomerID" />
<index column="Posn" />
<one-to-many class="Address" />
</list>
</class>
<class name="Address" table="Address">
<id name="id" column="ID" access="field">
<generator class="guid" />
</id>
</class>
Part of my my test:
Customer c = new Customer() { Name = "Hugo" };
c.AddAddress(new Address());
...
using (var s = sf.SessionFactory.OpenSession())
using (var tx = s.BeginTransaction())
{
s.Save(c);
tx.Commit();
}
Fails:
Cannot insert the value NULL into column 'Posn', Console output from
NHibernate:
INSERT INTO dbo.Customer (Name, ID) VALUES (@p0, @p1); @p0 = 'Hugo',
@p1 = 'ea9c30d7-782d-430a-9442-502ddd7978f5'
INSERT INTO dbo.Address (ID) VALUES (@p0); @p0 = '3010ae19-8807-476d-
ba4d-ac67f0167b35'
I'm using Nhibernate2Beta1, DB Sqlserver 2005 (express), which is
shaped like this:
Table Customer with ID (uniqueidentifier, PK), Name (nchar(10), not
null)
Table Address with ID (uniqueidentifier, PK), Posn (int, not null),
CustomerID (uniqueidentifier, FK -> Customer.ID)
I have seen examples on the internet that look very similar, notably
here:
http://sdesmedt.wordpress.com/2006/09/04/nhibernate-part-4-mapping-techniques-for-aggregation-one-to-many-mapping/
Then there is a passage that disrupts my confidence in the NHdocs 6.4
(one-to-many): "Very Important Note: If the <key> column of a <one-to-
many> association is declared NOT NULL, NHibernate may cause
constraint violations..."
Is that something that applies here? Do I really need to make the
above bidirectional? Can anyone enlighten me so that I can go to
weekend without heartpain and sadness?
Take care you lot,
Frank