I want to map the following company to the below address. I want it to be a one way relationship since the address class is being used by several other classes.
When I generate the schema the junction table is created successfully but when I try to save an entity nothing is persisted in the junction table. Only in address and company tables. What do I need to do to make this work?
public CompanyMapping()
{
Map(x => x.Name);
HasManyToMany(x => x.Addresses);
}
public AddressMapping()
{
Id(x => x.Id);
Map(x => x.AddressLineOne)
.Not.Nullable();
Map(x => x.AddressLineTwo)
.Nullable();
Map(x => x.PostCode)
.Not.Nullable();
Map(x => x.City)
.Not.Nullable();
}
public class HasManyToManyCascadeConvention : IHasManyToManyConvention
{
public void Apply(IManyToManyCollectionInstance instance)
{
instance.Cascade.All();
instance.Inverse();
instance.Key.ForeignKey(Inflector.Underscore("fk_" + instance.EntityType.Name + "_" + instance.ChildType.Name));
}
}
<class xmlns="urn:nhibernate-mapping-2.2" name="Company" table="`company´">
<id name="Id">
<column name="company_id" />
<generator class="identity" />
</id>
<bag table="company_address" lazy="false" cascade="save-update" name="Addresses">
<key column="company_id" />
<many-to-many class="Address" column="address_id" />
</bag>
<property name="Name">
<column name="name" />
</property>
</class>
EDIT 2010-11-05: If I add mappings for BillingAddress and DeliveryAddress like this I don't have any problems.
References(x => x.BillingAddress)
.Column("billing_address_id")
.ForeignKey("fk_account_billing_address")
.Cascade.All()
.Fetch.Join();
References(x => x.DeliveryAddress)
.Column("delivery_address_id")
.ForeignKey("fk_account_delivery_address")
.Cascade.All()
.Fetch.Join();