I use nhibernate 3.2 mapping by code and I’ve a strange behaviour.
If I simplify my project, I have 2 tables :
[Serializable]
public class Intervention
{
public Intervention()
{
ReponsePointVerification = new List<ReponsePointVerification>();
}
#region Public Properties
public virtual int Id
{
get;
set;
}
public virtual IList<ReponsePointVerification> ReponsePointVerification
{
get;
set;
}
}
public class InterventionMap : ClassMapping<Intervention>
{
public InterventionMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
Bag(x => x.ReponsePointVerification, map =>
{
map.Key( k => k.Column( "IdIntervention" ) );
});
}
}
and
[Serializable]
public class ReponsePointVerification
{
#region Public Properties
public virtual int Id
{
get;
set;
}
public virtual Intervention Intervention
{
get;
set;
}
}
public class ReponsePointVerificationMap : ClassMapping<ReponsePointVerification>
{
public ReponsePointVerificationMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
ManyToOne<Intervention>(x => x.Intervention, map => map.Column("IdIntervention"));
}
}
which represents two tables Intervention and ReponsePointVerification which has a foreign key (IdIntervention) which is linked to Intervention.
When I call :
return (from interv in Session.Query<Intervention>()
.Fetch(rep => rep.ReponsePointVerification)
select interv).ToList();
or even when I don’t fetch the information. I has this error :
Could not cast the value in field id0_ of type Int32 to the Type SerializableType. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.
The sql query looks to be ok.
I found that post http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc
And I modify the InterventionMap class according to that post it will work :
public class InterventionMap : ClassMapping<Intervention>
{
public InterventionMap()
{
Id<int>(x => x.Id, map =>
{
map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
Bag(x => x.ReponsePointVerification, map =>
{
map.Key( k => k.Column( "IdIntervention" ) );
}, rm => rm.OneToMany());
}
}
The only difference is the add of “, rm => rm.OneToMany()”
Trust me I’m very happy it worked but I really don’t understand that line can someone explain me the meaning ?
Regards
Edit
I made a WriteAllXmlMapping and the 2 codes are :
version without rm => rm.OneToMany() (which doesn’t work)
<bag name="ReponsePointVerification">
<key column="IdIntervention" />
<element type="Hyma.BusinessObjets.ReponsePointVerification" />
</bag>
version with rm => rm.OneToMany() (which work)
<bag name="ReponsePointVerification">
<key column="IdIntervention" />
<one-to-many class="ReponsePointVerification" />
</bag>
OneToManytells NH that this is a one-to-many relationship to another mapped entity. if left blank NH takesElementCollectionas default which is a one-to-many relationship to some ElementType without an id.for example when you have these classes
then NH would create these 3 tables
or vice versa, when you have the 3 Tables, it would be reasonable to map it to the classes