I am running a test project using NHibernate. I have two classes, Question and RateeGroup:
Question.cs
Code:
using System;
using System.Collections;
using System.Text;
namespace MVP.Data.Models
{
public class Question
{
public Question()
{
}
private int _Id;
private string _Name;
private string _ShortName;
private string _Text;
private IList _Groups;
private IList _Types;
public virtual int Id
{
get
{
return _Id;
}
}
public virtual string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public virtual string ShortName
{
get
{
return _ShortName;
}
set
{
_ShortName = value;
}
}
public virtual string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
}
}
RateeGroup.csCode:
using System;
using System.Collections;
using System.Text;
namespace MVP.Data.Models
{
public class RateeGroup
{
#region "Private Members"
private string m_Id;
private string m_SurveyId;
private string m_Name;
private string m_Description;
private bool m_Exclusive;
private IList m_Questions;
private IList m_Ratees;
#endregion
#region "Public Members"
public virtual string Id
{
get
{
return this.m_Id;
}
set
{
this.m_Id = value;
}
}
public virtual string SurveyId
{
get
{
return this.m_SurveyId;
}
set
{
this.m_SurveyId = value;
}
}
public virtual string Name
{
get
{
return this.m_Name;
}
set
{
this.m_Name = value;
}
}
public virtual string Description
{
get
{
return this.m_Description;
}
set
{
this.m_Description = value;
}
}
public virtual bool Exclusive
{
get
{
return this.m_Exclusive;
}
set
{
this.m_Exclusive = value;
}
}
public virtual IList Questions
{
get
{
return this.m_Questions;
}
set
{
this.m_Questions = value;
}
}
public virtual IList Ratees
{
get
{
return this.m_Ratees;
}
set
{
this.m_Ratees = value;
}
}
#endregion
#region "Public Methods"
public override bool Equals(object obj)
{
return base.Equals(obj);
//return ((this.m_Id == ((RateeGroup) obj).Id) && (this.m_SurveyId == ((RateeGroup) obj).SurveyId));
}
public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
}
}
RateeGroup is loaded from a table with two primary keys, RateeGroupID and SurveyID, so it is mapped with a composite key:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="MVP.Data.Models" namespace="MVP.Data.Models">
<class name="MVP.Data.Models.RateeGroup, MVP.Data.Models" table="RateeType" lazy="true">
<composite-id>
<key-property name="Id" column="RateeTypeID" />
<key-property name="SurveyId" column="SurveyID" />
</composite-id>
<property name="Name" />
<property name="Description" />
<property name="Exclusive" />
<bag name="Questions" table="AssessmentItemLink" lazy="true">
<key>
<column name="GroupID" />
<column name="AssessmentID" />
</key>
<many-to-many column="AssessmentItemID" class="Question" />
</bag>
</class>
</hibernate-mapping>
Question is a very straightforward mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="MVP.Data.Models" namespace="MVP.Data.Models">
<class name="MVP.Data.Models.Question, MVP.Data.Models" table="AssessmentItem" lazy="true">
<id name="Id" column="AssessmentItemID" access="nosetter.pascalcase-underscore">
<generator class="native" />
</id>
<property name="Name" />
<property name="ShortName" />
<property name="Text" />
</class>
</hibernate-mapping>
Question is the child of RateeGroup, but I am implementing a unidirectional relationship, RateeGroup -> Question.
RateeGroup is joined to Question through a join table called AssessmentItemLink.
The problem is this: when I attempt to load a RateeGroup, if the mapping finds Questions via the AssessmentItemLink table, NHibernate promptly deletes the rows that define the link between Question and RateeGroup!
What am I doing wrong here?