-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problems with self referencing objects and proxies
PostPosted: Tue Feb 20, 2007 7:23 pm 
Newbie

Joined: Tue Feb 20, 2007 5:41 pm
Posts: 5
Problem

I am using a three level object model where the second level subclass represents a bi-directional self referencing set of data.
The Parent ( Mother ) doesn't hyrate properly in this situation. While it has the values of the actual record all the member variables are null or default.

Code:
. . . . . . +-------+ . . . . .
. . . . . . |Entity | . . . . .
. . . . . . | . . . | . . . . .
. . . . . . +-------+ . . . . .
. . . . . . . .^. . . . . . . .
. . . . . . . .|. .+----+ . . .
. . . . . . +-------+ . | . . .
. . . . . . |Animal | . | . . .
. . . . . . | . . . |<--+ . . .
. . . . . . +-------+ . . . . .
. . . . . . . .^. . . . . . . .
. . . . . . . .|. . . . . . . .
. . . . +------+------+ . . . .
. . . . | . . . . . . | . . . .
. . . +---+ . . . . +---+ . . .
. . . |Cat| . . . . |Dog| . . .
. . . +---+ . . . . +---+ . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .


NHibernate version: 1.2.0 beta 3

Name and version of the database I am using: SQL 2005 SP1

Mapping documents:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Entity.Core.Entity,Entity.Core" table="Entity" lazy="false">
    <id name="Key" column="EntityId" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property column="IsActive" type="Boolean" name="IsActive" not-null="true" />
    <joined-subclass name="Entity.Core.Animal,Entity.Core" table="Animal">
      <key column="EntityId"/>
      <property column="Name" type="String" name="Name" length="50" />
      <bag name="Children" inverse="true" lazy="false" >
        <key column="Mother" />
        <one-to-many class="Entity.Core.Animal,Entity.Core" />
      </bag>
      <many-to-one name="Mother" cascade="none" column="Mother" class="Entity.Core.Animal,Entity.Core" />
      <joined-subclass name="Entity.Core.Cat,Entity.Core" table="Cat">
        <key column="EntityId"/>
      </joined-subclass>
      <joined-subclass name="Entity.Core.Dog,Entity.Core" table="Dog">
        <key column="EntityId"/>
      </joined-subclass>
    </joined-subclass>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:

protected void Page_Load(object sender, EventArgs e)
{
   ISession session = NHibernateSessionManager.Instance.GetSession();

   ITransaction tx = session.BeginTransaction();

   IQuery query = session.CreateQuery("select a from Entity as a where a.Key = :id");
   query.SetCharacter("id", '7');
   foreach (Core.Entity entity in query.Enumerable())
   {
      // This casts just fine
      Animal animal = entity as Animal;
      // Animal happens to be of type Cat
      if (animal != null)
      {
         // Cat cast to Dog = null normally a *good* but I suspect for the wrong reasons so *bad*
         Dog dogParent = animal.Mother as Dog;
         // Cat cast to Cat = null *bad*
         Cat catParent = animal.Mother as Cat;
         // Cat cast to Dog = null *good*
         Dog dogCurrent = animal as Dog;
         // Cat cast to Cat = not null *good*
         Cat catCurrent = animal as Cat;
         // Dog cast to Dog = not null *good*
         Dog dogChild = animal.Children[0] as Dog;
         // Dog cast to Cat = null *good*
         Cat catChild = animal.Children[0] as Cat;
      }
   }

   tx.Commit();

   session.Close();
}



Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:22 pm 
Newbie

Joined: Tue Feb 20, 2007 5:41 pm
Posts: 5
On tinkering a bit more (I have a killer headache now) ... I have noticed that when I explicitly turn lazy loading off on all classes and only leave it on on the collection of children. everything hydrates as expected, meaning that I can cast Cat to Cats and Dogs to Dogs properly.

It is possible (In fact probable) that I don't understand the intent of the lazy attribute in the HBM config but I would have expected that the default behaviour would that lazy loading would be off. If it does default to off how was I confusing it.

Anyhow here is the new HBM. It works although I'll have to think about it some more before I fully understand why.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Entity.Core.Entity,Entity.Core" table="Entity" lazy="false">
    <id name="Key" column="EntityId" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property column="IsActive" type="Boolean" name="IsActive" not-null="true" />
    <joined-subclass name="Entity.Core.Animal,Entity.Core" table="Animal" lazy="false">
      <key column="EntityId"/>
      <property column="Name" type="String" name="Name" length="50" />
      <bag name="Children" inverse="true" lazy="true" >
        <key column="Mother" />
        <one-to-many class="Entity.Core.Animal,Entity.Core" />
      </bag>
      <many-to-one name="Mother" cascade="none" column="Mother" class="Entity.Core.Animal,Entity.Core" />
      <joined-subclass name="Entity.Core.Cat,Entity.Core" table="Cat" lazy="false">
        <key column="EntityId"/>
      </joined-subclass>
      <joined-subclass name="Entity.Core.Dog,Entity.Core" table="Dog" lazy="false">
        <key column="EntityId"/>
      </joined-subclass>
    </joined-subclass>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:46 pm 
Newbie

Joined: Tue Feb 20, 2007 5:41 pm
Posts: 5
oh yeah I forgot the cast exception which you guys might find relevant

Unable to cast object of type 'CProxyTypeEntity_CoreAnimalCore_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2' to type 'Entity.Core.Dog'.

sorry for being such a dunce :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 2:36 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Read http://www.hibernate.org/hib_docs/nhibernate/html/performance.html#performance-proxies


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 3:14 am 
Newbie

Joined: Tue Feb 20, 2007 5:41 pm
Posts: 5
yep...that explains why it behaves that way.

thanks sergey I appreciate your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.