UsersEntityBase.cs
Code:
public class UsersEntityBase : BaseEntity {
private System.String _Fname = "";
public System.String Fname {
get { return this._Fname; }
set { this._Fname = value; }
}
private System.Int32 _Userid = 0;
public System.Int32 Userid {
get { return this._Userid; }
set { this._Userid = value; }
}
public UsersEntityBase (System.Int32 _Userid)
{ this._Userid = _Userid; }
private IList m_UserRoleEntities = null;
public IList UserRoleEntities
{
get
{
IList collection = (IList) InitializeProperty (m_UserRoleEntities);
if (collection == null) {
collection = new ArrayList();
this.m_UserRoleEntities = collection;
}
return collection;
}
set { m_UserRoleEntities = value; }
}
}
RolesEntityBase class is similar (replace "User" By "Role")
UserRoleEntityBase.csCode:
public class UserRoleEntityBase : BaseEntity {
private System.Int32 _Roleid = 0;
public System.Int32 Roleid {
get { return this._Roleid; }
set { this._Roleid = value; }
}
private System.Int32 _Userid = 0;
public System.Int32 Userid {
get { return this._Userid; }
set { this._Userid = value; }
}
/// <summary>
/// Default constructor.
/// </summary>
public UserRoleEntityBase ()
{}
public UserRoleEntityBase (System.Int32 _Userid, System.Int32 _Roleid )
{
this._Userid = _Userid;
this._Roleid = _Roleid;
}
private RolesEntity RolesBy_Roleid = null;
public RolesEntity RolesByRoleid {
get { return this.RolesBy_Roleid ; }
set { this.RolesBy_Roleid = value; }
}
private UsersEntity UsersBy_Userid = null;
public UsersEntity UsersByUserid {
get { return this.UsersBy_Userid ; }
set { this.UsersBy_Userid = value; }
}
}
Mapping Files:
Users.hbm.xmlCode:
<?xml version="1.0" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Sly.Csrs.Data.UsersEntity, DAO" table="Users">
<id name="Userid" column="userid">
<generator class="identity"/>
</id>
<bag name="UserRoleEntities" inverse="true" lazy="true" cascade="all" >
<key column="userid" />
<one-to-many class="Sly.Csrs.Data.UserRoleEntity, DAO" />
</bag>
<property name="Fname" column="fname" not-null="true" />
</class>
</hibernate-mapping>
Roles.hbm.xml is almost identical.
UserRole.hbm.xmlCode:
<?xml version="1.0" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Sly.Csrs.Data.UserRoleEntity, DAO" table="UserRole">
<composite-id>
<key-property name="Userid" column="userid" />
<key-property name="Roleid" column="roleid" />
</composite-id>
<many-to-one name="RolesByRoleid" column="roleid" class="Sly.Csrs.Data.RolesEntity, DAO" insert="false" update="false" not-null="true" />
<many-to-one name="UsersByUserid" column="userid" class="Sly.Csrs.Data.UsersEntity, DAO" insert="false" update="false" not-null="true" />
</class>
</hibernate-mapping>
Sample codeCode:
UsersEntity user = new UsersEntity ();
user.FName = Guid.NewGuid().ToString();
using (ISession session = factory.OpenSession())
{
session.SaveOrUpdateCopy(user);
session.Flush();
}
UserRoleEntity userRole = new UserRoleEntity ();
userRole.RolesByRoleid = new RolesEntity ();
userRole.UsersByUserid = user;
user.UserRoleEntities.Add(userRole);
using (ISession session = factory.OpenSession())
{
// Error occured on the line below
session.SaveOrUpdateCopy(userRole);
session.Flush();
}
When I look to the db profiler I see that NHibernate tries to select using following query (before this it checks that such row doesn't exist yet):
Code:
INSERT INTO UserRole (userid, roleid) VALUES (0, 0)
What is wrong?
Thanks!