Multiple tables\objects in one nHibernate mapping
- by Morrislgn
Hi Folks
I am trying to create an nHibernate mapping for a class structure like so:
class UserDetails{
Guid id;
User user;
Role role;
public User UserInfo{
get;set;
}
public Role UserRoles{
get;set;
}
public Guid ID{
Get; set;
}
}
class User{
string name;
int id;
public string Name{
get;set;
}
public int ID{
get;set;
}
}
class Role{
string roleName;
string roleDesc;
int roleId;
public string RoleName{
get;set;
}
public string RoleDesc{
get;set;
}
public int RoleID{
get;set;
}
}
The underlying DB structure is similar to the tables, but there is a linking table which links user and role using their respective IDs:
UserRoleLinkTable[
identity User_Role_ID (pk)
userID (FK to User table)
roleid (FK to Role table)
]
After playing about with nHibernate this is similar to what I want to try and achieve (but it doesnt work!):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="Admin"
namespace="Admin"
>
<class name="UserDetails" lazy="false" table="USER">
<id name="ID">
<generator class="guid"></generator>
</id>
<one-to-one name="UserInfo" class="User" lazy="false" cascade="none"/>
<bag
name="UserRoles"
inverse="false"
table="Role"
lazy="false"
cascade="none"
>
<key column="Role" />
<many-to-many class="Role" column="ROLE_ID" />
</bag>
</class>
</hibernate-mapping>
I have mappings\entities which appear to work for Role and User (used in other aspects of the project) objects but how do I pull this information into one UserDetails class? The point of the user details to be able to return all this information together as one object. Is it possible to create (for want of a better description) a container using an nHibernate mapping and map the data that way?
Hopefully there is enough info to help work this out - thanks in advance for all help given!
Cheers,
Morris