简体   繁体   中英

Multiple tables\objects in one nHibernate mapping

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

By means of an update to this post I have since realised that this cant be done and that I was thinking about the modelling the wrong way.

I went and created a remodelled the UserRoles class so that it inherited from User and had a property called Roles instead. This was a much easier solution to implement, so problem sorted out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM