NH seemingly ignoring access="field" in mapping.

23 views
Skip to first unread message

Enrique Ramirez

unread,
Mar 30, 2009, 1:53:02 AM3/30/09
to nhu...@googlegroups.com
Good day,

I'm having an issue where I'm trying to map a private member of a
class which happens to have a non-virtual public setter Property. I
noticed that even if I set the access="field" attribute in the
property tag in the mappings file, as long as NHibernate sees my
public setter property, it gives an InvalidProxyTypeException. If i
make my setter property private or virtual public, then everything
works as planned.

I'm curious though, isn't NHibernate supposed to ignore properties if
I specify access="field"? I also tried nosetter and got the same
behavior. Also tried specifying the name strategy and same result.

Code snippets below:

Mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name="Test" table="test" lazy="true" proxy="Test">
<id name="id" column="id" type="int" access="field.lowercase-underscore">
<generator class="sequence">
<param name="sequence">test_id_seq</param>
</generator>
</id>
<property name="_problematicfield" column="problematicfield"
type="string" length="50" access="field" not-null="true" />
</class>
</hibernate-mapping>

Class:
public class Test
{
public Test () {}

private int _id;
private string _problematicfield;

public string ProblematicField
{
set
{
_problematicfield =DoSomethingInteresting(value);
}
}
}

--
------------------------------------------------------------------
Enrique Ramirez Irizarry
Lead Developer, CodaBytes Consulting Group
http://www.Codabytes.com
Personal Page: http://www.indiecodelabs.com

Anne Epstein

unread,
Mar 30, 2009, 2:32:29 AM3/30/09
to nhu...@googlegroups.com
At least as far as 2.0.1 goes, if you're using lazy loading for a
mapped class, everything public in the class needs to marked as
virtual, regardless of access settings. That includes all properties,
including properties not mapped at all, and any methods as well.
Reason is roughly that NHibernate wants to make a complete proxy, and
can't do that if it can't override everything in the class.

Kenneth Siewers Møller

unread,
Mar 30, 2009, 3:28:43 AM3/30/09
to nhu...@googlegroups.com
Try disable lazy loading for the class.

2009/3/30 Anne Epstein <aje...@gmail.com>

Fabio Maulo

unread,
Mar 30, 2009, 10:04:37 AM3/30/09
to nhu...@googlegroups.com
NH is ignoring the property.
Who not ignore it is the proxy-system and you if you want work with lazy-loading.
You can disable the proxy validation trough a configuration property, after that you have the responsibility to check the "proxyability" of your class.

2009/3/30 Enrique Ramirez <enri...@gmail.com>



--
Fabio Maulo

Enrique Ramirez

unread,
Mar 30, 2009, 11:09:42 AM3/30/09
to nhu...@googlegroups.com
Anne, Kenneth, and Fabio, thanks so much for guiding me in the right
direction. Disabled lazy loading and removed the "proxy" attribute and
now it's properly using the access attribute.

Fabio Maulo

unread,
Mar 30, 2009, 11:42:16 AM3/30/09
to nhu...@googlegroups.com
Well... remove lazy-loading is not a solution.

2009/3/30 Enrique Ramirez <enri...@gmail.com>



--
Fabio Maulo

Enrique Ramirez

unread,
Mar 30, 2009, 1:51:40 PM3/30/09
to nhu...@googlegroups.com
On Mon, Mar 30, 2009 at 11:42 AM, Fabio Maulo <fabio...@gmail.com> wrote:
> Well... remove lazy-loading is not a solution.

After putting some thought into it, this class will be a foreign key
to the "workhorse" class. So you do have a point in that removing lazy
load is not a good solution in the long run. The reason why I didn't
want the setter method to be virtual was because it calculates values
for other private fields, and I was calling it in one of the class
constructors. I guess I could make a helper method to provide this
functionality and be able to place it in both the constructor and the
virtual setter.

Unless I'm missing something I think that's my only option.

Fabio Maulo

unread,
Mar 30, 2009, 1:59:29 PM3/30/09
to nhu...@googlegroups.com
protected

2009/3/30 Enrique Ramirez <enri...@gmail.com>



--
Fabio Maulo

Enrique Ramirez

unread,
Mar 30, 2009, 3:57:38 PM3/30/09
to nhu...@googlegroups.com
On Mon, Mar 30, 2009 at 1:59 PM, Fabio Maulo <fabio...@gmail.com> wrote:
> protected

Ah, of course. I should have thought of that two days ago. Thanks again!

Kenneth Siewers Møller

unread,
Mar 30, 2009, 5:04:20 PM3/30/09
to nhu...@googlegroups.com
It wasn't a solution, merely a pointer in the right direction ;)

2009/3/30 Fabio Maulo <fabio...@gmail.com>
Reply all
Reply to author
Forward
0 new messages