views:

456

answers:

2

I am new to Nhibernate and I am using Nhibernate 2.1.0 RC1. In C# I have the following classes:

public class Application
{
    public virtual int Id { get; set; }
    public virtual Applicant Applicant { get; set; }
}

public class Applicant
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual IList<Application> Applications { get; set; } //maybe i should use set to eliminate duplicates
}

And I have the following database schema in SQL Server 2005:

Applications table
{
  ApplicationId int PK IDENTITY NOTNULL
  FK_ApplicantId int FK NOTNULL
}

Applicants table
{
  ApplicantId int PK IDENTITY NOTNULL
  FirstName string NOTNULL
  LastName string NOTNULL
}

And I have the following Nhibernate mapping file:

I need bidirectional mapping :

  • 1 applicant can have > 1 application
  • 1 application belongs to 1 applicant

I have no idea how to map Application collection to Applicant. Please help. Thanks! Also I am not using Fluent Nhibernate as it doesn't seem to support Nhibernate 2.1.0 RC1 yet.

Updated (this is the working version):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="HelloWorld"
                   namespace="HelloWorld">

  <class name="Application" table="Applications">
    <id name="Id" column="ApplicationID" />
    <property name="Reference" />

    <many-to-one name="Applicant" column="ApplicantID" not-null="true"/>
  </class>

  <class name="Applicant" table="Applicants">
    <id name="Id" column="ApplicantID" />
    <property name="FirstName" column="FirstName" />
    <property name="LastName" column="LastName" />

    <set name="Applications" inverse="true"> <!-- good to remove this mapping -->
      <key column="ApplicantID"/>
      <one-to-many class="Application"/>
    </set>
  </class>
</hibernate-mapping>

I would also remove the Application collection from Applicant object to reduce the change of load thousands of applications an applicant lodged. Reason for this is here..

+1  A: 

There is something wrong with your heirarchy. Why does an Applicant have a collection of Applications where each application is assigned to another applicant? Please recheck your objects and the relationships that you are trying to build. You can just have an Application class and an Applicant Class and an Application can have mulitple applications just the way it is by simply having the applicant ID in the Application class and in the database you can have a many to one relationship between application and applicant indicating that there can be multiple applications tied to an applicant.

CodeToGlory
I need bidirectional mapping (what's wrong with the below mapping?):- 1 applicant can have > 1 application- 1 application belongs to 1 applicant
Jeffrey C
I get what you are saying now.
Jeffrey C
A: 
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="HelloWorld"
                   namespace="HelloWorld">

  <class name="Application" table="Applications">
    <id name="Id" column="ApplicationID" />
    <property name="Reference" />

    <many-to-one name="Applicant" column="ApplicantID" not-null="true"/>
  </class>

  <class name="Applicant" table="Applicants">
    <id name="Id" column="ApplicantID" />
    <property name="FirstName" column="FirstName" />
    <property name="LastName" column="LastName" />

    <set name="Applications" inverse="true">
      <key column="ApplicantID"/>
      <one-to-many class="Application"/>
    </set>
  </class>
</hibernate-mapping>
Jeffrey C