过滤 SQL 查询中的子列表结果

发布于 2024-12-22 00:52:39 字数 1285 浏览 2 评论 0原文

我有一个菜单系统,其中 MenuTabs - 顶层,它们包含 Menu ,并且 Menu 包含 MenuItem

每个级别项目都有一个对其可见的用户角色列表。

我需要创建一个数据库查询来选择 MenuTabs,其中仅包含当前用户的可见菜单和可见菜单项。有什么办法可以用SQL查询来实现吗?

我使用 JPA 和 MySql,但任何 SQL 查询都会有帮助。

作为输入数据,我有当前用户的角色列表,我需要为他/她构建正确的菜单系统。因此,用户将只看到允许的 MenuTabs / Menus / MenuItems

类:

class MenuTab { 
   List<Menu> menu; 
   List<Role> userRoles;
}

class Menu {
   List<MenuItem> menuItems; 
   List<Role> userRoles;
}

class MenuItem {
   List<Role> userRoles;
}

class User {
   List<Role> userRoles;
}

表格:

menutab            menutab_menu              menutab_role
  id                  menutabId                 menutabId
                      menuId                    roleId


menu               menu_menuitem             menu_role
  id                  menuId                    menuId
                      menuitemId                roleId


menuitem           menuitem_role
  id                  menuitemId
                      roleId


role
   id

user                user_role
   id                 userId
                      roleId

I have a menu system where MenuTabs - the top level, they contain Menu and Menu contains MenuItem s.

Each level item has a list of user Role s it should be visible for.

I need to create a database query to select MenuTabs which contains only visible Menus with visible MenuItems for the current User. Is any way to do it with SQL query?

I use JPA and MySql, but any SQL query would be helpful.

As input data, I have a list of roles for the current user and I need to build right menu system for him/her. So user will see only allowed MenuTabs / Menus / MenuItems

Classes:

class MenuTab { 
   List<Menu> menu; 
   List<Role> userRoles;
}

class Menu {
   List<MenuItem> menuItems; 
   List<Role> userRoles;
}

class MenuItem {
   List<Role> userRoles;
}

class User {
   List<Role> userRoles;
}

Tables:

menutab            menutab_menu              menutab_role
  id                  menutabId                 menutabId
                      menuId                    roleId


menu               menu_menuitem             menu_role
  id                  menuId                    menuId
                      menuitemId                roleId


menuitem           menuitem_role
  id                  menuitemId
                      roleId


role
   id

user                user_role
   id                 userId
                      roleId

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

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

发布评论

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

评论(2

十级心震 2024-12-29 00:52:39

如果您使用 Hibernate,则不必处理 sql
我不熟悉 Hibernate,但在 NHibernate 中你可以像这样使用 HQL:

var _query = DetachedCriteria.For<MenuTab>()
                                .Add(Restrictions.In("userRoles", listOfRoles)
                                .Add(Restrictions.In("MenuItem.userRoles", listOfRoles);

if you are using Hibernate you dont have to deal with sql
i am not familiar with Hibernate but in NHibernate you could utilize HQL like that :

var _query = DetachedCriteria.For<MenuTab>()
                                .Add(Restrictions.In("userRoles", listOfRoles)
                                .Add(Restrictions.In("MenuItem.userRoles", listOfRoles);
纵山崖 2024-12-29 00:52:39

在 SQL 中,我可能会这样做:

SELECT
  ...
FROM menutab_menu tm
  INNER JOIN menu_menuitem mi ON tm.menuId     = mi.menuId
  INNER JOIN menutab t        ON tm.menutabId  = t.Id
  INNER JOIN menu m           ON tm.menuId     = m.Id
  INNER JOIN menuitem i       ON mi.menuitemId = i.Id
WHERE
  t.Id IN (
    SELECT tr.menutabId
    FROM menutab_role tr
      INNER JOIN user_role ur ON tr.roleId = ur.roleId
    WHERE ur.userId = @userId
  )
  AND m.Id IN (
    SELECT mr.menutabId
    FROM menu_role mr
      INNER JOIN user_role ur ON mr.roleId = ur.roleId
    WHERE ur.userId = @userId
  )
  AND i.Id IN (
    SELECT ir.menutabId
    FROM menuitem_role ir
      INNER JOIN user_role ur ON ir.roleId = ur.roleId
    WHERE ur.userId = @userId
  )

假设 @userId 是当前用户 ID,它作为参数传递给查询。

In SQL I might perhaps do something like this:

SELECT
  ...
FROM menutab_menu tm
  INNER JOIN menu_menuitem mi ON tm.menuId     = mi.menuId
  INNER JOIN menutab t        ON tm.menutabId  = t.Id
  INNER JOIN menu m           ON tm.menuId     = m.Id
  INNER JOIN menuitem i       ON mi.menuitemId = i.Id
WHERE
  t.Id IN (
    SELECT tr.menutabId
    FROM menutab_role tr
      INNER JOIN user_role ur ON tr.roleId = ur.roleId
    WHERE ur.userId = @userId
  )
  AND m.Id IN (
    SELECT mr.menutabId
    FROM menu_role mr
      INNER JOIN user_role ur ON mr.roleId = ur.roleId
    WHERE ur.userId = @userId
  )
  AND i.Id IN (
    SELECT ir.menutabId
    FROM menuitem_role ir
      INNER JOIN user_role ur ON ir.roleId = ur.roleId
    WHERE ur.userId = @userId
  )

It is assumed that @userId is the current user ID, which is passed to the query as a parameter.

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