Hi, I am new to NHibernate and have run in to some problems with my first test project.
I have the following classes:
Code:
public class BaseDomainObject
{
private int _Id = 0;
public BaseDomainObject()
{
}
protected BaseDomainObject(int id)
{
_Id = id;
}
public int Id
{
get
{
return _Id;
}
}
}
public class GolfCourseInfo : BaseDomainObject
{
private string _Name;
public GolfCourseInfo()
{
}
public GolfCourseInfo(int id, string name) : base(id)
{
_Name = name;
}
public string Name
{
get
{
return _Name;
}
}
}
public class GolfCourse : GolfCourseInfo
{
private IList _Holes;
public GolfCourse()
{
}
public GolfCourse(int id, string name, IList holes) : base(id, name)
{
_Holes = holes;
}
public IList Holes
{
get
{
return _Holes;
}
}
}
And here are the mappings:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
<class name="GarageDev.GolfBuddy.Domain.GolfCourseInfo, GarageDev.GolfBuddy.Domain" table="GolfCourse">
<id name="Id" column="fldId" type="System.Int32" access="nosetter.pascalcase-underscore">
<generator class="assigned" />
</id>
<property name="Name" column="fldName" type="System.String" access="nosetter.pascalcase-underscore"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
<class name="GarageDev.GolfBuddy.Domain.GolfCourse, GarageDev.GolfBuddy.Domain" table="GolfCourse">
<id name="Id" column="fldId" type="System.Int32" access="nosetter.pascalcase-underscore">
<generator class="assigned" />
</id>
<property name="Name" column="fldName" type="System.String" access="nosetter.pascalcase-underscore"/>
<bag name="Holes" inverse="false" table="GolfCourseHole" order-by="fldNumber" access="nosetter.pascalcase-underscore">
<key column="fldGolfCourseId" />
<one-to-many class="GarageDev.GolfBuddy.Domain.Hole, GarageDev.GolfBuddy.Domain" />
</bag>
</class>
</hibernate-mapping>
And the code:
Code:
public IList GetAllGolfCourses()
{
using (ISession session = factory.OpenSession())
{
return session.CreateCriteria(typeof(GolfCourse)).AddOrder(NHibernate.Expression.Order.Asc("Name")).List();
}
}
public IList GetAllGolfCourseInfos()
{
using (ISession session = factory.OpenSession())
{
return session.CreateCriteria(typeof(GolfCourseInfo)).AddOrder(NHibernate.Expression.Order.Asc("Name")).List();
}
}
When I call the first method I get the expected result 3 items, but when I call the second I get six. When I ran the SQL Profiler I noticed that NHibernate makes calls for both GolfCourse and GolfCourseInfo when calling the second method. What have I done wrong?
Regards
Pelle