问题描述
场景
我一直在敲打我的头靠在墙上,试图找出正确的映射为3个实体:用户,角色和权限。在我的应用程序,用户可以有特权,这只是给用户更多的权限。用户还可以有角色的是基本上需要附加属性权限。
I've been banging my head against the wall trying to figure out the correct mapping for 3 entities: User, Role, and Privilege. In my application, Users can have Privileges, which just give a user additional permissions. Users can also have Roles which are essentially privileges that require additional properties.
有关例如,用户可能具有应用程序管理员的作用,在这种情况下,ApplicationAdministratorRole.cs需要一个属性包含的应用程序的列表,用户可以管理。用户还可以拥有事件管理,在这种情况下,Privilege.cs将不包含任何附加属性的事件,因为在我们的应用程序事件管理员可以管理所有事件的特权。我希望这个例子是有道理的。如果没有,我可以详细多一点。
For instance, a user might have a role of "Application Administrator" in which case the ApplicationAdministratorRole.cs would need a property to contain the list of applications that a user can manage. A user could also have a privilege of "Event Administrator" in which case Privilege.cs would NOT contain any additional properties for events because in our application an event administrator can manage all events. I hope this example makes sense. If not, I can elaborate a little more.
表结构
[表名称]
TBL_USERS
[Table Name]TBL_USERS
[专栏]
用户ID(PK),
名字,
姓氏,
CompanyId,
等...
[Columns]UserId (PK),FirstName,LastName,CompanyId,etc...
[表名称]
TBL_ROLEREF(只定义系统内的角色)
[Table Name]TBL_ROLEREF (just defines the roles within the system)
[色谱柱]
角色ID(PK),
ROLENAME
[Columns]RoleId (PK),RoleName
[表名称] TBL_USERROLES(表交叉引用用户角色)
[Table Name] TBL_USERROLES (table to cross reference users to roles)
[专栏]
UserRoleId(PK),
用户ID,
角色ID,
ActiveDate,
DeactiveDate
[Columns]UserRoleId (PK),UserId,RoleId,ActiveDate,DeactiveDate
[表名称] TBL_APPLICATIONADMINISTRATORS
[Table Name] TBL_APPLICATIONADMINISTRATORS
[专栏]
ApplicationAdministratorId(PK),
的applicationID,
用户ID,
角色ID,
ActiveDate,
DeactiveDate
[Columns]ApplicationAdministratorId (PK),ApplicationId,UserId,RoleId,ActiveDate,DeactiveDate
[表名称] TBL_PRIVILEGEREF
[Table Name] TBL_PRIVILEGEREF
[专栏]
PrivilegeId (PK),
PrivilegeName
[Columns]PrivilegeId (PK),PrivilegeName
[表名称] TBL_USERPRIVILEGES
[Table Name] TBL_USERPRIVILEGES
[专栏]
UserPrivilegeId(PK),
用户ID,
PrivilegeId,
ActiveDate,
DeactiveDate
[Columns]UserPrivilegeId (PK),UserId,PrivilegeId,ActiveDate,DeactiveDate
表结构是相当直接的,所有权限和角色有一个ActiveDate和DeactiveDate等等我们可以保持用户以前的角色和特权的历史。有一点要注意的是,任何角色需要一个额外的表来存储与此角色一起去的任何其他信息,在这种情况下,TBL_APPLICATIONADMINISTRATORS将配合用户应用程序管理员的角色,从TBL_APPLICATIONREF不同的应用。再次,请让我知道如果我需要改口这更好的感觉。
The table structure is pretty straight forward, all privileges and roles have an ActiveDate and DeactiveDate so that we can maintain a history of a users previous roles and privileges. One thing to note is that any role requires an additional table to store any additional information that goes along with this role, in this case, TBL_APPLICATIONADMINISTRATORS will tie a user's Application Administrator role to different applications from TBL_APPLICATIONREF. Again, please let me know if I need to reword this to make better sense.
映射文件
[User.hbm.xml]
[User.hbm.xml]
User对象应该有特权和角色的集合。这些袋子也许应该集,但对于这个例子来说,我不认为它应该没关系。
User objects should have a collection of Privileges and Roles. These bags should probably be sets, but for the sake of this example I don't think it should matter.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.User, Core" table="TBL_USERS">
<id name="UserId" column="USERID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERS</param>
</generator>
</id>
<property name="Title" column="USERTITLE" type="string" length="50" not-null="false" />
<property name="FirstName" column="USERFIRSTNAME" type="string" length="50" not-null="true" />
<property name="LastName" column="USERLASTNAME" type="string" length="50" not-null="true" />
<bag name="Privileges" generic="true" table="TBL_USERPRIVILEGES">
<key column="USERID" />
<many-to-many column="PRIVILEGEID" class="Core.Entities.Privilege, Core" />
</bag>
<bag name="Roles" generic="true" table="TBL_USERROLES" >
<key column="USERID" />
<many-to-many column="ROLEID" class="Core.Entities.Role, Core" />
</bag>
</class>
</hibernate-mapping>
[Privilege.hbm.xml]
[Privilege.hbm.xml]
权限对象应该有一个PrivilegeId,PrivilegeName,与特权相关用户的集合,并ActiveDate / DeactiveDate。我注释掉了我的失败尝试映射此
Privilege objects should have a PrivilegeId, PrivilegeName, a collection of users associated with the privilege, and ActiveDate/DeactiveDate. I have commented out my failed attempts to map this.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.Privilege, Core" table="TBL_PRIVILEGEREF">
<id name="PrivilegeId" column="PRIVILEGEID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERPRIVILEGES</param>
</generator>
</id>
<property name="Name" column="PRIVILEGENAME" type="string" length="128" not-null="false" />
<!--
This does not work. NHibernate is complaining about the repeated Column "USERID"
I have made several attempts to get this to work with no luck... where am I going wrong?
<bag name="Users" generic="true" table="TBL_USERPRIVILEGES" inverse="true">
<key column="USERID" />
<many-to-many column="USERID" class="Core.Entities.User, Core" />
</bag>
-->
<!--
This also does not work. This was my attempt to join the ActiveDate and DeactiveDate into Privilege.cs
The join NHibernate creates with this setup is completely wrong...
<join table="TBL_USERPRIVILEGES">
<key column="USERPRIVILEGEID" />
<property name="ActiveDate" column="ACTIVEDATE" type="DateTime" not-null="false" />
<property name="DeactiveDate" column="DEACTIVEDATE" type="DateTime" not-null="false" />
</join>
-->
</class>
</hibernate-mapping>
[Role.hbm.xml]
[Role.hbm.xml]
我想有一个有ActiveDate和DeactiveDate每个角色(如ApplicationAdministratorRole)可以从让每一个角色被强制具有这些属性继承性角色的基类。我想我可以用一个接口来强制执行此为好,但这是我在映射的东西半复杂的NHibernate的,所以请给我一些这方面的方向第一枪。
I would like to have a Role base class that has properties for ActiveDate and DeactiveDate that each role (like ApplicationAdministratorRole) can inherit from so that every role is forced to have these properties. I suppose I could use an interface to enforce this as well, but this is my first shot at mapping something semi-complicated in NHibernate, so please give me some direction on this.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.Role, Core" table="TBL_ROLEREF">
<id name="RoleId" column="ROLEID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERROLES</param>
</generator>
</id>
<property name="Name" column="ROLENAME" type="string" length="128" not-null="false" />
<bag name="Users" generic="true" table="TBL_USERROLES">
<key column="ROLEID" />
<many-to-many column="USERID" class="Core.Entities.User, Core" />
</bag>
<joined-subclass name="Core.Entities.ApplicationAdministratorRole, Core" table="TBL_APPLICATIONADMINISTRATORS" extends="Core.Entities.Role, Core">
<key column="ROLEID" />
<property name="ApplicationAdministratorId" column="APPLICATIONADMINISTRATORID" type="Int32" />
<bag name="Applications" generic="true" table="TBL_APPLICATIONREF">
<key column="APPLICATIONID" />
<one-to-many class="Core.Entities.Application, Core" />
</bag>
</joined-subclass>
<!-- Do I need to use <join> here to set the ActiveDate and DeactiveDate in the Role base class? -->
</class>
</hibernate-mapping>
我已经做了很多的阅读这些东西,但我明显失去了一些东西。正如你可能已经收集,我实现一个表每个子类的战略角色。
I've done a lot of reading on this stuff, but I'm obviously missing something. As you may have gathered, I am implementing a table-per-subclass strategy for Roles.
这些映射文件的工作,因为他们,但他们不返回正确的结果。任何及所有帮助是极大的赞赏。
These mapping files work as they are, but they do not return the correct results. Any and all help is greatly appreciated.
谢谢你们,
约什 -
推荐答案
正在尝试使用USERID时的两倍,同时作为密钥,并作为参考用户映射。
你大概的意思是:
You are trying to map the Users when using USERID twice, both as the key and as the reference.You probably mean:
我不明白这是什么TBL_PRIVILEGEREF和TBL_USERPRIVILEGES之间的关系,但似乎并不被任何:
I don't understand this, what is the relation between TBL_PRIVILEGEREF and TBL_USERPRIVILEGES, there doesn't seems to be any:
您加入子类映射似乎罚款
You join-subclass mapping seems fine
这篇关于NHibernate的映射用户角色和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!