I have the following error as part of my nhibernate project;
Could not find a setter for property '0' in class 'AdHockey.Models.User'
The following is my model;

    public class User {
        /// <summary>
        /// Unique identifier for group used by program and database layer. 
        /// </summary>
        public virtual int UserId {
            get; set;
        }

        /// <summary>
        /// User's first name. 
        /// </summary>
        public String FirstName {
            get; set;
        }

        /// <summary>
        /// User's last name. 
        /// </summary>
        public String LastName {
            get; set;
        }

        /// <summary>
        /// Used to turn off user accessibility for instance when there are forensic investigations going on. 
        /// </summary>
        public bool IsActive {
            get; set;
        }

        /// <summary>
        /// A hash of the users password is stored in the database and used for logins. 
        /// Storing a hash is more secure than storing plaintext. 
        /// No Company should have a comprehensive plaintext wordlist of it's users. 
        /// SHA-256 with a byte array storage size of 256
        /// </summary>
        public byte[] PasswordHash {
            get; set;
        }

        /// <summary>
        /// The groups a user is associated with. 
        /// </summary>
        public IList<Group> Groups {
            get; set;
        }

        /// <summary>
        /// The reports a user is scheduled to recieve. 
        /// </summary>
        public IList<Report> Reports {
            get; set;
        }

        public User() {
            IsActive = true;
        }
    }//end class

The following is my mapping file;

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> <class name="AdHockey.Models.User, AdHockey" table=""USER"" lazy="false" schema="cblock"> <id name="UserId" access="property" type="int" column="USER_ID"> <generator class="native"/> </id> <property name="FirstName" access="property" column="FIRST_NAME" length="64" /> <property name="LastName" access="property" column="LAST_NAME" length="64" /> <property name="PasswordHash" access="property" column="PASSWORD_HASH" length="256" /> <!-- IsActive property used for locking accounts. --> <property name="IsActive" access="property" column="IS_ACTIVE" length="64" /> </class> </hibernate-mapping>

And the following is a method from my repository;

        public IList<User> GetGroupUsersPaged(int groupId, int pageNumber, int pageSize) {
            List<User> users = null;
            ISession session = OpenSession();
            using (ITransaction tx = session.BeginTransaction()) {
                String query = "SELECT usr\r\n"
                             + "FROM Group as grp\r\n"
                             + " JOIN grp.Users as usr\r\n"
                             + "WHERE grp.GroupId = :groupId\r\n";
                users = (List<User>)session.CreateQuery(query)
                        .SetInt32("groupId", groupId)
                        .SetFirstResult((pageNumber - 1) * pageSize)
                        .SetMaxResults(pageSize)
                        .SetResultTransformer(Transformers.AliasToBean(typeof(User)))
                        .List<User>();
            }

            if (users == null)
                return new List<User>();

            return users;
        }//end method

The code executes fine the first time, but not the second time. The first time it gets an empty list, but the second time it throws the error after I have added a user to the repository. If anyone has seen this problem before I could use some help.

Recommended Answers

All 3 Replies

what is this supposed to be:

public virtual int UserId {
            get; set;
        }

?

Is there a reason why this part of your mapping file:

table=""USER"" 

has two double quotes?

Actually those two things appear to be right. It looks like when I posted the xml danni's site converted & quot; to actual quotation marks. The Quotation marks are specified because in my oracle database the USER keyword is a reserved word, so it needs escaped in the mapping file. I have removed the keyword virtual from the user primary key, but it still does not work. My appologies for the condition of the markup, the site seems to want to compress xml.

I don't know exactly how I got this to work, but I think I used more linq than the previous example. I will post everything of import;

        public List<User> GetGroupUsersPaged(int groupId, int pageNumber, int pageSize) {
            List<User> users = null;
            ISession session = OpenSession();
            using (ITransaction tx = session.BeginTransaction()) {
                users = session.Query<Group>()
                        .Where(grp => grp.GroupId == groupId)
                        .SelectMany(grp => grp.Users)
                        .Skip((pageNumber - 1) * pageSize)
                        .Take(pageSize)
                        .ToList();
            }

            if (users == null)
                return new List<User>();

            return users;
        }//end method

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="AdHockey.Models.User, AdHockey" table="&quot;USER&quot;" lazy="false" schema="cblock">
    <id name="UserId" access="property" type="int" column="USER_ID">
      <generator class="native"/>
    </id>

    <property name="FirstName" access="property" column="FIRST_NAME" length="64" />
    <property name="LastName" access="property" column="LAST_NAME" length="64" />
    <property name="PasswordHash" access="property" column="PASSWORD_HASH" length="256" />

    <property name="TelephoneNumber" access="property" column="TELEPHONE_NUM" length="12" />
    <property name="EmailAddress" access="property" column="EMAIL_ADDRESS" length="64" />

    <!-- IsActive property used for locking accounts. -->
    <property name="IsActive" access="property" column="IS_ACTIVE" length="64" />

     <!--Many to many relationship with user table.--> 
    <bag name="Groups" access="property" lazy="false" table="GRP_USR_BRIDGE" cascade="none" schema="cblock" inverse="false">
      <key>
        <column name="USER_ID" />
      </key>
      <many-to-many column="GROUP_ID" class="AdHockey.Models.Group, AdHockey" />
    </bag>

     <!--Many to many relationship with user table.--> 
    <bag name="Reports" access="property" lazy="false" table="RPT_USR_BRIDGE" schema="cblock" inverse="false">
      <key>
        <column name="USER_ID" />
      </key>
      <many-to-many column="REPORT_ID" class="AdHockey.Models.Report, AdHockey" />
    </bag>

  </class>
</hibernate-mapping>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Configuration;

namespace AdHockey.Models {
    public class User {
        /// <summary>
        /// Unique identifier for group used by program and database layer. 
        /// </summary>
        public int UserId {
            get; set;
        }

        /// <summary>
        /// User's first name. 
        /// </summary>
        public String FirstName {
            get; set;
        }

        /// <summary>
        /// User's last name. 
        /// </summary>
        public String LastName {
            get; set;
        }

        /// <summary>
        /// Used to turn off user accessibility for instance when there are forensic investigations going on. 
        /// </summary>
        public bool IsActive {
            get; set;
        }

        /// <summary>
        /// The user's email address. 
        /// </summary>
        public String EmailAddress {
            get; set;
        }

        /// <summary>
        /// Telephone number of the user. 
        /// </summary>
        public String TelephoneNumber {
            get; set;
        }

        /// <summary>
        /// A hash of the users password is stored in the database and used for logins. 
        /// Storing a hash is more secure than storing plaintext. 
        /// No Company should have a comprehensive plaintext wordlist of it's users. 
        /// SHA-256 with a byte array storage size of 256
        /// </summary>
        public byte[] PasswordHash {
            get; set;
        }

        /// <summary>
        /// The groups a user is associated with. 
        /// </summary>
        public IList<Group> Groups {
            get; set;
        }

        /// <summary>
        /// The reports a user is scheduled to recieve. 
        /// </summary>
        public IList<Report> Reports {
            get; set;
        }

        public User() {
            IsActive = true;
        }

        public void SetPassword(String password) {
            //get salt from web config
            byte[] salt = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["salt"].ToString());
            byte[] passBytes = Encoding.UTF8.GetBytes(password);

            //perpend salt to password
            byte[] catPass = salt.Concat(passBytes).ToArray();

            //call all the hash algorithims here
            HashAlgorithm hashAlg = SHA512.Create();
            this.PasswordHash = hashAlg.ComputeHash(catPass);
        }//end method

        public bool ComparePassword(String password) {
            //get salt from web config
            byte[] salt = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["salt"].ToString());
            byte[] passBytes = Encoding.UTF8.GetBytes(password);

            //perpend salt to password
            byte[] catPass = salt.Concat(passBytes).ToArray();

            //call all the hash algorithims here
            HashAlgorithm hashAlg = SHA512.Create();

            byte[] incomingHash = hashAlg.ComputeHash(catPass);

            if (incomingHash.SequenceEqual(this.PasswordHash))
                return true;

            return false;
        }//end method

        public bool HasAccess(Report report) {
            foreach (var usr in report.Users)
                if (report.Users.Contains(usr))
                    return true;

            return false;
        }//end method
    }//end class

}//end namespace
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.