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: Do not save class property
PostPosted: Mon Nov 24, 2008 8:17 am 
Newbie

Joined: Fri Nov 14, 2008 7:43 am
Posts: 7
Hi there!

I am having a problem related to nhibernate ( at least i think so ... ) .Currently I am working with WCF, so I had to create my own proxies, based on my domain model wich has several classes, for instance - User class wich has a List that point to the latests user accesses and so on . The problem here is that IList (nhibernate collection ) can't be serialized by WCF and so it gaves me an error . What I thought was to create an additional field called accessArray wich is my list but in the array form . I have done that, and initialized the list and the array correctly, the problem is that nhibernate is asking for the virtual keyword on property get/set .

User.cs :
Code:

    [DataContract]
    public class User :  IUser
    {
        private int id;

        [DataMember]
        public virtual int Id
        {
            get { return id; }
            set { id = value; }
        }

        private IList<Access> lstAccess;

        [DataMember]
        public Access[] accessArray { get; set; }

        public virtual IList<Access> LstAccess
        {
            get { return lstAccess; }
            set { lstAccess = value; }
        }

        private State userState;

        private string username;

        [DataMember]
        public virtual string Username
        {
            get { return username; }
            set { username = value; }
        }

        private string password;

        [DataMember]
        public virtual string Password
        {
            get { return password; }
            set { password = value; }
        }

        private Profile userProfile;

        [DataMember]
        public virtual Profile UserProfile
        {
            get { return userProfile; }
            set { userProfile = value; }
        }

        [DataMember]
        public virtual State UserState
        {
            get { return userState; }
            set { userState = value; }
        }

        public User(string username, string password)
        {
            LstAccess = new List<Access>();

            this.username = username;
            this.password = password;
            this.userState = State.Enabled;
        }

        public User()
        {
            LstAccess = new List<Access>();
        }

        private string EncryptPass(string pwd)
        {
            var hasher = new MD5CryptoServiceProvider();
            byte[] tBytes = Encoding.ASCII.GetBytes(pwd);
            byte[] hBytes = hasher.ComputeHash(tBytes);


            var sb = new StringBuilder();
            for (var c = 0; c < hBytes.Length; c++)
                sb.AppendFormat("{0:x2}", hBytes[c]);

            return (sb.ToString());
        }


        public override string ToString()
        {
            var userData = new StringBuilder();
            userData.Append("Id." + this.id);
            userData.Append(" Username :" + this.username);
            userData.AppendLine("Acessos : ");

            foreach (var c in this.lstAccess)
            {
                userData.AppendLine(c.ToString());
            }

            return userData.ToString();
        }

        #region IUser Members

        public virtual IUser Login()
        {
            return UserDAO<User>.Instance().Login(username,EncryptPass(this.password));
        }


        public virtual bool SaveNewUser()
        {
            this.password = EncryptPass(this.password);
            return GenericDAO<User>.Instance().Add(this);
        }


        public virtual bool UpdateExistingUser()
        {
            return GenericDAO<User>.Instance().Update(this);
        }


        public virtual bool DeleteExistingUser()
        {
            return GenericDAO<User>.Instance().Remove(this);
        }


        public virtual bool DeleteAllAccesses()
        {
            return GenericDAO<Access>.Instance().Remove(this.lstAccess);
        }

        #endregion
    }


    [DataContract]
    public enum State
    {

        [EnumMember]
        Disabled = 0,

        [EnumMember]
        Enabled = 1
    }




And my error log :

A first chance exception of type 'NHibernate.InvalidProxyTypeException' occurred in NHibernate.DLL
NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
Save.Domain.User: method get_accessArray should be 'public/protected virtual' or 'protected internal virtual'
Save.Domain.User: method set_accessArray should be 'public/protected virtual' or 'protected internal virtual'
at NHibernate.Cfg.Configuration.Validate() in d:\My Documents\OSS\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 812
at NHibernate.Cfg.Configuration.BuildSessionFactory() in d:\My Documents\OSS\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 998
at Save.NHibernate.NHibernateHelper.get_SessionFactory() in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\NHibernate\NHibernateHelper.cs:line 22
at Save.NHibernate.NHibernateHelper.OpenSession() in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\NHibernate\NHibernateHelper.cs:line 31
at Save.DAL.UserDAO`1.Login(String username, String password) in D:\My Documents\Visual Studio 2008\Projects\Save-Backup\Save\Save\DAL\UserDAO.cs:line 61
The program '[4288] WebDev.WebServer.EXE: Managed' has exited with code 0 (0x0).
The program '[3860] iexplore.exe: Silverlight' has exited with code 0 (0x0).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 9:19 pm 
Newbie

Joined: Mon Jun 23, 2008 6:38 pm
Posts: 13
Try this:

[NonSerialized]
private IList<Access> lstAccess;

public virtual Access[] accessArray
{
get { return new List(lstAccess).toArray(); }
}

[XmlIgnore]
public virtual IList<Access> LstAccess
{
get { return lstAccess; }
set { lstAccess = value; }
}


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.