I've got a working mapping on two tables with single PK and FK. But I cannot find a solution for a Composite Key of Address and Id in table contact. I read something about an extra class, e.g. "AddressContactKey". How would this work?
Code:
namespace NewNHibernate
{
class Contact
{
public String Id { get; set; }
public Address Address { get; set; }
public string Name1 { get; set; }
public string Name2 { get; set; }
public string Name3 { get; set; }
public Contact() { }
public Contact(Address address)
{
Id = "NameOfContact";
Address = address;
}
}
}
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" default-lazy="false">
<class name="NewNHibernate.Contact, NewNHibernate">
<id name="Id" type="string" >
<generator class="assigned"/>
</id>
<property name="Name1" />
<property name="Name2" />
<property name="Name3" />
<many-to-one name="Address" class="NewNHibernate.Address" />
</class>
</hibernate-mapping>
Code:
namespace NewNHibernate
{
class Address
{
public string Name { get; set; }
public string Name2 { get; set; }
private ISet contacts = new HashedSet();
public ISet Contacts
{
get { return contacts; }
set { contacts = value; }
}
}
}
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" default-lazy="false">
<class name="NewNHibernate.Address, NewNHibernate">
<id name="Name" type="string">
<generator class="assigned"/>
</id>
<property name="Name2" />
<set name="Contacts" inverse="true" cascade="all-delete-orphan">
<key column="Address" />
<one-to-many class="NewNHibernate.Contact, NewNHibernate" />
</set>
</class>
</hibernate-mapping>