-->
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.  [ 2 posts ] 
Author Message
 Post subject: Can NHibernate use protected setters?
PostPosted: Mon Apr 26, 2010 12:01 pm 
Newbie

Joined: Tue Jun 17, 2008 3:26 am
Posts: 7
I read in the documentation something like : "Properties need not be declared public - NHibernate can persist a property with an internal, protected, protected internal or private visibility."

So, I guess NH should be able to access the property even if the setter is protected. But that is not working for me. I am assuming that the above statement means I don't have to change the access strategy or the naming strategy.
Example:
Class
Code:
public class Candidate
    {
        virtual public string Number { get; set; }
        virtual public DateTime AddedOn { get; protected set; }
        virtual public Template Template { get; set; }
        virtual public string ZoneName { get; set; }
        virtual public DateTime LastActivity { get; set; }

        protected Candidate()
        {

        }
        public Candidate
            (string number, TemplateType candidateFor, DateTime addedOn, string zoneName)
        {
            this.Number = number;
            this.AddedOn = addedOn;
            this.ZoneName = zoneName;
        }
        public override bool Equals(object obj)
        {
            Candidate that = obj as Candidate;
            if (that == null) return false;
            return this.Number.Equals(that.Number);
        }
        public override int GetHashCode()
        {
            return this.Number.GetHashCode();
        }
    }

Here is the mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MNP_Contracts" namespace="MNP_Contracts">
    <class name="Candidate" table="Candidates" dynamic-update="false" lazy="false">
        <id name="Number" column="Number">
            <generator class="assigned" />
        </id>
        <property name="AddedOn" column="AddedOn" />
        <property name="ZoneName" column="ZoneName" />
        <property name="LastActivity" column="LastActivity" />
        <many-to-one name="Template" column="TemplateID" />
    </class>
</hibernate-mapping>


When loading objects of the above class, the AddedOn property is not set correctly.
What am I missing here?


Top
 Profile  
 
 Post subject: Re: Can NHibernate use protected setters?
PostPosted: Tue Apr 27, 2010 5:07 am 
Newbie

Joined: Tue Jun 17, 2008 3:26 am
Posts: 7
Scratch the question, turns out I was misusing a feature in WCF where protected properties are not serialized by default (using inferred DataContract), so the solution was to attribute the class with [DataContract] and each property with [DataMember].
Sorry for a misplaced question.


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