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.