I have to map two simple table with a foreign key relationship. One of the tables is Contact containing columns id (primary key of type int),name, address and guid (newly added and is not the primary key). The other one is phone__number containing columns id (primary key of type int), contact___id (foreign key of id in contact table) and phone__number.
The mapping file for Contact table is as below :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OfflineDbSyncWithNHibernate" default-lazy="true" namespace="OfflineDbSyncWithNHibernate.Models">
<class name="Contact" table="Contact">
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="Name" column="name" type="string"/>
<property name="Address" column="address" type="string"/>
<property name="Guid" column="guid" type="string"/>
<set lazy="true" batch-size="6" table="phone_number" name="PhoneNumbers" fetch="join" inverse="false" cascade="all" >
<key foreign-key="FK_contact_phone_number" column="contact_id"/>
<one-to-many class="PhoneNumber" />
</set>
</class>
</hibernate-mapping>
The mapping file for Phone_number table is :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OfflineDbSyncWithNHibernate" default-lazy="true" namespace="OfflineDbSyncWithNHibernate.Models">
<class name="PhoneNumber" table="phone_number">
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="ContactId" column="contact_id" />
<property name="Number" column="phone_number" />
</class>
</hibernate-mapping>
The Contact and PhoneNumber classes are :
namespace OfflineDbSyncWithNHibernate.Models
{
public class Contact
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Address { get; set; }
public virtual string Guid { get; set; }
public virtual PhoneNumbers PhoneNumbers { get; set; }
}
}
namespace OfflineDbSyncWithNHibernate.Models
{
public class PhoneNumber
{
public virtual int Id { get; set; }
public virtual int ContactId { get; set; }
public virtual string Number { get; set; }
}
}
namespace OfflineDbSyncWithNHibernate.Models
{
public class PhoneNumbers : List<PhoneNumber>
{
}
}
When I load the contact and phone_numbers separately it works, but after adding the set element to get a one-to-many relationship nhibernate is giving an error :
NHibernate.MappingException: Invalid mapping information specified for type OfflineDbSyncWithNHibernate.Models.Contact, check your mapping file for property type mismatches
I am new to nHibernate so I am not sure if there is a mistake in the set element or I should not even be using it. Any help will be appreciated.