-->
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.  [ 4 posts ] 
Author Message
 Post subject: Proxy fetch on invocation of identifier getter
PostPosted: Mon May 05, 2008 10:24 am 
Newbie

Joined: Fri Apr 25, 2008 5:03 am
Posts: 2
NHibernate 1.2.1

The NHibernate doc states: 'Proxy fetching - a single-valued association is fetched when a method other than the identifier getter is invoked
upon the associated object'

This does not work for me. Any access to the proxy hits the database. Here's my example code:
---
Article art = (Article)session.Load(typeof(Article), 10990);
Console.WriteLine(art.Number);
---
I get the same behavior for proxies loaded for a to-one association - a get on the identity property fires the proxy!


Putting 'show_sql' to 'true' shows an SELECT on the article. If I commend out the Console.WriteLine line, the code behaves as I would expect - no select.

Is the documentation wrong or should this work?

Source
--------
namespace Ista.Amm.Application.BusinessObject
{
public partial class Article
{
protected Article() {}
public Article(int number)
{
this._number = number;
}
private int _number;
virtual public int Number
{
get { return _number; }
}
private string _description;
virtual public string Description
{
get { return _description; }
set { _description = value ; }
}
....

Mapping file
-------------
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BusinessObjects" namespace="Ista.Amm.Application.BusinessObject">
<class name="Article" table="VW1Article">
<id name="Number" access="field.camelcase-underscore" column="number_">
<generator class="assigned"/>
</id>
<property name="Description" access="field.camelcase-underscore" column="description" not-null="true"/>
<many-to-one name="BaseArticle" access="field.camelcase-underscore" class="BaseArticle" unique="true">
<column name="baseArticleNumber"/>
</many-to-one>
<property name="ZeroSettingDevice" access="field.camelcase-underscore" column="zeroSettingDevice" type="YesNo" not-null="false"/>
<property name="NumberOfSensors" access="field.camelcase-underscore" column="numberOfSensors" not-null="false"/>
<property name="VolumeterType" access="field.camelcase-underscore" column="volumeterType" type="Ista.Amm.Application.BusinessObject.VolumeterType, BusinessObjects" not-null="false"/>
<property name="DisplayUnit" access="field.camelcase-underscore" column="displayUnit" type="Ista.Amm.Application.BusinessObject.DisplayUnit, BusinessObjects" not-null="false"/>
</class>
</hibernate-mapping>



DEBUG output:
-----
2008-05-05 16:12:40,127 [DEBUG] NHibernate.Impl.SessionImpl loading [Article#10990] (11)
2008-05-05 16:12:40,393 [DEBUG] NHibernate.Impl.SessionImpl attempting to resolve [Article#10990] (11)

Without invoking the getter the second line does not show up!


Top
 Profile  
 
 Post subject: Proxy fetch on invocation of identifier getter
PostPosted: Tue May 06, 2008 4:36 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I don't think NHibernate can do that unless you use the standard access="property" for the identifier. Otherwise NHibernate isn't aware of the accessor (int Number {get;}).

If you change the access to 'property' (the default), then the problem should go away. If you're worried about giving clients access to the mutator (the setter), then the mutator can be marked as protected.

Code:
<id name="Number" column="number_">


Code:
private int _number;

virtual public int Number
{
    get { return _number; }
    protected set { _number = value; }
}


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 4:36 am 
Newbie

Joined: Fri Apr 25, 2008 5:03 am
Posts: 2
Yes, this helps. I acually changed the mapping to:
---
<id name="Number" access="nosetter.camelcase-underscore" column="number_">
---
to tell Hibernate to use the Getter for getting and direct field access for setting. And everything works as specified.

Access = "property" is not working for me because I may have logic in the Setter.


Top
 Profile  
 
 Post subject: Proxy fetch on invocation of identifier getter
PostPosted: Thu May 08, 2008 8:40 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Cool ... I didn't know about that option.

Thanks for sharing your solution.

Cheers,
Richard


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