-->
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: Mapping to private field w/ access="field" & n
PostPosted: Mon Sep 05, 2005 3:58 pm 
Newbie

Joined: Fri Sep 02, 2005 1:11 am
Posts: 4
Are there any good examples of mapping in a way that NHibernate uses the private property, while the API users use a public property.

The reason for doing this is so that I can add logic to the public getter/setter property, but I don't want this logic to occur when NHibernate is persisting the object. In my particular case, I'm one-way crypting a password field whenever the API user sets it.

I've found references to access="field" and also that I might need to use a naming strategy. This is mentioned for cases where you might need, say, a read-only field in your API. I can't seem to find any good examples of the syntax, though.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 5:22 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Hi jasonh,

It's actually pretty straight forward. You could create a class like so:

Code:
   public class Customer
   {
      //NHibernate accessors
      private int      id            = 0;
      private string   name         = null;
      private string   phoneNumber      = null;

      //Properties
      public int Id
      {
         get { return id; }
         set { id = value; }
      }

      public string Name
      {
         get { return name; }
         set { name = value; }
      }

      public string PhoneNumber
      {
         get { return phoneNumber; }
         set { phoneNumber = value; }
      }
   }


Note that the NHibernate accessors are all private and I've provided a set of properties that are public which are in turn referring to the NHibernate accessors. You could add any other logic you wanted to in the getters and setters of course.

This class could be mapped with the following mapping file:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Framework.Entities" assembly="Framework">
   
   <class name="Customer" table="Customers" lazy="true">
      
      <id name="id" column="CustomerId" unsaved-value="0" access="field">
         <generator class="native" />
      </id>
      
      <property name="name" column="CustomerName" access="field"/>
      <property name="phoneNumber" column="Telephone" access="field" />
      
   </class>
   
</hibernate-mapping>


You'll notice that the <property> mappings "name" attribute uses the name of the NHibernate accessor in the class (must be matching case!) and the "access" attribute is set to "field" so that it looks for private fields rather than trying to map to the public properties on the class.

Hope this helps,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 5:27 pm 
You will need the naming strategy when using properties if your field name differs from your property name. We underscore everything but I don't expose properties for my id's so my mapping looks like:

Code:
    <id name="Id" column="storelId" type="Int32" unsaved-value="0" access="nosetter.camelcase-underscore">
       <generator class="native" />
    </id>


Code:
   private int _id;

   public int Id
   {
      get { return _id; }
   }

[/b]


Top
  
 
 Post subject: Thanks
PostPosted: Thu Sep 08, 2005 1:03 am 
Newbie

Joined: Fri Sep 02, 2005 1:11 am
Posts: 4
Thanks. I knew it was something relatively simple, I just couldn't get it to work.

I was reading about differing opinions on mapping hibernate directly to private fields - as far as best OO practices go or whatever. I wound up creating a setter method on my object in addition to the property, In my case I call SetPassword() which does the crypting. There is still a Password property that's mapped.

The negative is that I don't have any programatic way to prevent an API consumer from just setting the Password property directly, though.

I'm curious how others might approach this?


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.