具有隐含和显式关系的多对多

发布于 2024-11-25 16:21:31 字数 419 浏览 6 评论 0原文

我的数据库中 Person 和 Widget 之间有一个标准的多对多关系。具有管理角色的人员可以访问所有小部件。在我的应用程序中,我想查看一个人可以访问哪些小部件。

我有两个高级选项:

  1. 显式管理关系。当某人成为管理员时,将该人与所有现有的小部件相关联。创建小部件后,将该小部件与所有现有管理员相关联。

  2. 在运行时,如果该人员是管理员,则假定他们有权访问所有小部件,并在加载小部件时绕过关系表。

一种选择比另一种更好吗?这种情况有名字吗?

我一直在尝试使用 NHibernate 应用选项 2,但我似乎不知道如何让它在加载实体的所有小部件时绕过关系表(甚至如果可以的话,这将不必要地加载大量信息,除非我与 Person 实体分开加载小部件并应用分页)。

I have a standard many-to-many relationship in my database between Person and Widget. A Person in an administrative role has access to ALL Widgets. In my application, I want to see which Widgets a Person has access to.

I have two high level options:

  1. Explicitly manage the relationships. When a Person becomes an administrator, relate that Person to all existing Widgets. When a Widget is created, relate that Widget to all existing administrators.

  2. At run-time, if the Person is an administrator, assume they have access to ALL widgets and bypass the relationship table when loading Widgets.

Is one option better than the other? Is there a name for this scenario?

I have been trying to apply option 2 using NHibernate and I can't seem to figure out how to get it to bypass the relationship table when loading all Widgets for an entity (and even if I could, this would unnecessarily load alot of information unless I load Widgets separately from the Person entity and apply paging).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2024-12-02 16:21:31

我会通过角色来映射它。

角色 : 人员 = 1 : 许多

因此,当您创建人员时,您还会创建一个新角色,除非他们是管理员,在这种情况下,他们使用现有的管理员角色。

那么问题就很简单了:你需要一个 WidgetRole 表。

创建新的 Widget 时,条目会自动添加到 NewWidget、AdminRole 的 WidgetRole 表中。

当人员更改为管理员角色时,只需更改其当前角色即可。

在我看来,这个设置在逻辑上比特殊的管理案例更简单。

I would map this by means of Roles.

Roles : Person = 1 : Many

So when you create a person, you also create a new Role, unless they are an Administrator in which case they use the existing Admin Role.

Then the problem is easy: You need a WidgetRole table.

When a new Widget is created, and entry is automatically added to the WidgetRole table for NewWidget, AdminRole

When a Person changes to an Admin Role, simply change their current Role.

imo this setup is logically simpler, than having a special Admin case.

猥琐帝 2024-12-02 16:21:31

我必须分享最终的解决方案 - 应该帮助任何试图强制 NH 加载数据库中不明确的关系的人。

我已经创建了 person 的子类,并且 NHibernate 足够聪明,可以识别 Administrator : Person 并将该 Person 实例化为管理员(其中 Administrator 有一个带有 PK/FK PersonId 的表)

我刚刚为 Administrator 添加了一个新的映射覆盖...

mapping.HasManyToMany(x => x.Widgets)
  .Table("AdministratorWidgetAccess")
  .Cascade.None();

我添加了一个名为 AdministratorWidgetAccess 的视图...

SELECT a.PersonId as [AdministratorId], w.WidgetId as 
FROM dbo.Administrator AS a LEFT OUTER JOIN
  dbo.Widget AS w ON 1 = 1

运行时,如果该人是管理员,它会根据视图中的关系加载所有小部件,否则它会根据连接表加载小部件。

I had to share the final solution - should help anyone trying to force NH to load up a relationship that is not explicit in the DB.

I was already creating sub classes of person and NHibernate is smart enough to recognize Administrator : Person and instantiate that Person as an Administrator (where Administrator has a table with a PK/FK PersonId)

I just added a new mapping override for Administrator...

mapping.HasManyToMany(x => x.Widgets)
  .Table("AdministratorWidgetAccess")
  .Cascade.None();

And I added a view called AdministratorWidgetAccess...

SELECT a.PersonId as [AdministratorId], w.WidgetId as 
FROM dbo.Administrator AS a LEFT OUTER JOIN
  dbo.Widget AS w ON 1 = 1

When running, if the Person is an Administrator it loads up all Widgets based on the relationship in the view, otherwise it loads up Widgets based on the join table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文