I'm having trouble mapping my M:1 relationship between Petition and PetitionSignature in NHibernate. This is a type of relationship I've mapped before, but I just can't get it working right now.
The method I am trying to call in my provider is designed to return a collection of PetitionSignatures based on the Petition object that was passed in:
public IList<PetitionSignature> GetByPetition(Petition p)
{
IList<PetitionSignature> Signatures = (IList<PetitionSignature>)_session.CreateCriteria(typeof(PetitionSignature))
.Add(Restrictions.Eq("Petition", p))
.AddOrder(Order.Desc("Id")).List();
return Signatures.OrderBy(sig => sig.Date).ToList();
}
My Petition class is defined as:
public class Petition : IPetition
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual string Text { get; set; }
public virtual string Slug { get; set; }
public virtual DateTime Date { get; set; }
public virtual IList<PetitionSignature> Signatures { get; set; }
}
My PetitionSignature class is defined as:
public class PetitionSignature : IPetitionSignature
{
public virtual int Id { get; set; }
public virtual Petition Petition { get; set; }
public virtual string Name { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual string StreetAddress { get; set; }
public virtual DateTime Date { get; set; }
public virtual bool OkToEmail { get; set; }
}
My Petition mapping document is:
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="Title" column="Title"></property>
<property name="Text" column="Text"></property>
<property name="Date" column="Date"></property>
<property name="Slug" column="Slug"></property>
<bag name="Signatures" lazy="true" cascade="all-delete-orphan" inverse="true">
<key column="Id"/>
<one-to-many class="PetitionSignature"/>
</bag>
And my PetitionSignature mapping document is as follows:
<property name="Name" column="Name"></property>
<property name="EmailAddress" column="EmailAddress"></property>
<property name="Date" column="Date"></property>
<property name="OkToEmail" column="OkToEmail"></property>
<property name="PhoneNumber" column="PhoneNumber"></property>
<property name="StreetAddress" column="StreetAddress"></property>
<many-to-one name="Petition" class="Petition" column="Petition" cascade="all" />
These are the columns of my Petition table:
And these are the columns of my PetitionSignature table:
So here's what happens. I run my integration test in debug mode and test this out. I hydrate my Petition object with no trouble, and pass it to this method in the PetitionSignatureProvider class. That's when this happens:
Of course, this conversion can be made. Creating another method in the provider that accepts a PetitionId, the conversion is made fine. This leads me to believe the problem is with my mapping, shown above.
Any ideas?