These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help with inheritance greatly appreciated.
PostPosted: Wed Aug 02, 2006 6:45 am 
Newbie

Joined: Wed Aug 02, 2006 5:53 am
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: Help with inheritance greatly appreciated.
PostPosted: Wed Aug 02, 2006 7:36 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
pelle_h wrote:
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?


The subclasses must not be mapped using the <class> mapping, but with <subclass> mapping (If You want to use table-per-class hierarchy mapping)

For details, see http://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.