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.  [ 7 posts ] 
Author Message
 Post subject: How to: map to a private field in C#
PostPosted: Fri Aug 11, 2006 8:09 am 
Newbie

Joined: Tue Aug 08, 2006 5:51 am
Posts: 9
Hi,

I'm trying to map a private field in my class using NHibernate mapping attributes in C#.

It works fine when I use a public property, but when I switch to the private field belonging to that property I get the following exceptions:
{"Problem trying to set property type by reflection"} >
{"Could not find a getter for property 'user_ID' in class 'HibernateTest.User'"}

Apparently NHibernate.Mappings always explicitly looks for properties.
I also noticed that NHibernate.Mappings seems to ignore the C# entities to which the mapping attributes are added, as if they are simply piled together.
There are numerous mentions on the internet about the ability to map private fields, but I can't find a single example.

Your help is much appreciated.
Regards, Tonn


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 11:01 am 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
To map to a field you need to specify it using the access attribute="field" as the default is property.

<property name="user_ID"
column="user_id"
type="System.String"
access="field" />

It should not matter if the field or property is private, protected or public.
Also, if you flip back and forth in your mapping, just make sure you update the name as well (so that it points to the actual field or property name).

class User{
private string user_ID;

public string User_ID{
get{return user_ID;}
}

In this case if you put:
<property name="User_ID"
column="user_id"
type="System.String"
access="field" />

you would get errors because the field is user_ID not User_ID. (I've done that a few times when I don't want to have a set for a property)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 2:12 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
Sorry I guess you want to use the PropertyAttribute right in the class.

class User{

[NHibernate.Mapping.Attributes.PropertyAttribute(
column="user_id", type="System.String", access="field")]
private string user_id;

public string User_ID{
get{return user_id;}
}
}

Hope that helps


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 12, 2006 9:10 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I have a question regarding mapping to the field via the xxx.hbm.xml file.

I have a base class that all my persistent classes inherit. This class contains the primary key _Id field.

Since this field is not part of the subclasses, can I still map to the field or do I need a private property that only NHibernate uses to set this value?

Since I'm using native sequening, I don't want anyone to be able to provide the Id value directly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 6:11 am 
Newbie

Joined: Tue Aug 08, 2006 5:51 am
Posts: 9
Hi Padlewski,
Spot on! Thanks very much.


gcook,
Now that I've got it working, I have no problem moving my (primary key) id field and id property to a base class. It still works with no additional tweaking.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 1:33 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
Hi gcook1,

You can access a field in an inheritence structure. Also if your using .NET 2.0 you can have a public getter and a protected or private setter. Because your extending off of your base class, you will have to make the field or the setter protected so the child classes can access the data (but I'm not 100% sure).

Also, if you want to use lazy loading they have to be virtual.

Hope this answers your question,
Darin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 3:34 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
Actually,

I was able to keep the attributes of my base class (Id) private and NHibernate is still able to map them direct-to-field instead of going through the properties.

This is the method I prefer and is consistent with one of the more popular ORM tools in Java TOPLink for Java.

Now more than ever, I'm happy with my choice to use NHibernate.


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