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: Bug with <component> mapping and proxies (NH 1.2)
PostPosted: Sat Sep 22, 2007 6:17 am 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
I implement category hierarchy using the nested set model. For that a have the following <component> declaration in the Category.hbm.xml

Code:
<component name="NodeInfo" class="NHibernateHelper.NestedSet.NodeInfo, NHibernateHelper" update="true">
   <parent name="Node" />
   <property name="Left" type="Int64">
      <column name="ns_left" sql-type="int" not-null="true" />
   </property>
   <property name="Right" type="Int64">
      <column name="ns_right" sql-type="int" not-null="true" />
   </property>
   <property name="Depth" type="Int32" formula="(SELECT Count(*)
                                      FROM Categories c
                                      WHERE c.ns_left &lt; ns_left AND
                                      c.ns_right > ns_left)" />
</component>


The Category.cs has a property

Code:
public NodeInfo NodeInfo
{
   get { return _nodeInfo; }
   set { _nodeInfo = value; }
}


and the NodeInfo looks like:

Code:
public class NodeInfo
{
   private INode _node;

   private long _left = 0;
   private long _right = 0;
   private int _depth = 0;
   
   public NodeInfo() { }
   
   public NodeInfo(INode node)
   {
      _node = node;
   }

   public INode Node
   {
      get { return _node; }
      protected internal set { _node = value; }
   }

   public long Left
   {
      get { return _left; }
      protected internal set { _left = value; }
   }

   public long Right
   {
      get { return _right; }
      protected internal set { _right = value; }
   }

   public int Depth
   {
      get { return _depth; }
      protected internal set { _depth = value; }
   }
}


When I get an instance from the database everything is OK. Left, Right and Depth are filled normally. If I try to do something like:

Code:
if (product.Category.Depth == 1) { ... }


, "Depth" has always the default value (0). It seems that in lazy initialization, the components properties are not filled by the proxy. To be sure that this is a bug I tried the following:

Code:
session.Get(typeof(Category), 2); // assuming that product.Category.Id = 2

if (product.Category.Depth == 1) { ... }


, which works OK, as the instance has already been filled by .Get. As you can imagine setting the product-category relation as [lazy="false" fetch="join"] solves this issue, but I think it should be consider a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 6:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate should have complained that your properties are not virtual, did it?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 6:40 am 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
No it didn't!

I am a little embarassed that I didn't think that myself, although an exception should be raised.

I made NodeInfo virtual and now it works

Code:
public virtual NodeInfo NodeInfo
{
   get { return _nodeInfo; }
   set { _nodeInfo = value; }
}


Should I declared Node, Left, Right and Depth (in the NodeInfo class) virtual too? It seems thats it's not needed.

Thx for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 7:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Then you are either not using NHibernate 1.2, or you have disabled the proxy validator. No, you don't need to make the component's properties virtual.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 11:57 am 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
I'm pretty sure that I use v1.2 :).

If I remove the virtual keyword from any other property, an exception is raised. Only in the case of the <component> it doesn't.

I don't know if it matters but NodeInfo is a member of an Interface implementation.


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.