EJB-QL 3 中的 MEMBER OF 不起作用

发布于 2024-09-15 06:36:19 字数 1196 浏览 2 评论 0原文

我想检索许多具有一个共同“角色”的“访问”。

这是命名查询:

SELECT access 
FROM Access AS access 
WHERE :role MEMBER OF access.listRole

Access 实体

public class Access implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String libelle;

    @ManyToOne
    private Module oneModule;
    @ManyToMany
    private List<Role> listRole;
    /* Setter & Getter */
}

Role 实体

public class Role implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;
    @Enumerated(EnumType.STRING)
    private Flag oneFlag;
    @Transient
    private int lengthAccess;

    @OneToMany(mappedBy="oneRole")
    private List<UserAccount> listUserAccount;
    @ManyToMany
    private List<Access> listAccess;

    /* Geter & Setter */
}

但我没有实现正确的 EJB-QL !

配置:

  • EJB 3
  • MySQL (InnoDB)
  • jBoss
  • Plop

谢谢。

I would like to retrieve many 'Access' which have one 'Role' in common.

It's the named query:

SELECT access 
FROM Access AS access 
WHERE :role MEMBER OF access.listRole

The Access entity

public class Access implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String libelle;

    @ManyToOne
    private Module oneModule;
    @ManyToMany
    private List<Role> listRole;
    /* Setter & Getter */
}

The Role entity

public class Role implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;
    @Enumerated(EnumType.STRING)
    private Flag oneFlag;
    @Transient
    private int lengthAccess;

    @OneToMany(mappedBy="oneRole")
    private List<UserAccount> listUserAccount;
    @ManyToMany
    private List<Access> listAccess;

    /* Geter & Setter */
}

But I don't achieve to do the right EJB-QL !

Configuration:

  • EJB 3
  • MySQL (InnoDB)
  • jBoss
  • Plop

Thanks.

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

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

发布评论

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

评论(2

鸠魁 2024-09-22 06:36:19

我无法重现该问题。运行您提供的 JPQL 查询时,如下所示:

String qlString = "SELECT access " + 
                    "FROM Access AS access " + 
                   "WHERE :role MEMBER OF access.listRole";

Role role = new Role();
role.setId(1L);

List accesses = session.createQuery(qlString).setParameter("role", role).list();

Hibernate 为我生成以下 SQL 查询(我通过删除一些属性简化了您的实体):

select
    access0_.id as id127_,
    access0_.libelle as libelle127_,
    access0_.name as name127_ 
from
    Access access0_ 
where
    ? in (
        select
            role2_.id 
        from
            Access_ROLES listrole1_,
            ROLES role2_ 
        where
            access0_.id=listrole1_.Access_id 
            and listrole1_.listRole_id=role2_.id
    )

一切似乎都是正确的(使用 Hibernate Core 3.3.0.SP1、Hibernate Annotations 3.4 进行测试)。 0.GA,Hibernate EM 3.4.0.GA)

您使用的到底是什么版本的 Hibernate(核心、注释、EntityManager)?你到底得到了什么错误?您能展示如何调用查询吗?

I cannot reproduce the problem. When running the JPQL query you provided, like this:

String qlString = "SELECT access " + 
                    "FROM Access AS access " + 
                   "WHERE :role MEMBER OF access.listRole";

Role role = new Role();
role.setId(1L);

List accesses = session.createQuery(qlString).setParameter("role", role).list();

Hibernate generates the following SQL query for me (I simplified a bit your entities by removing some attributes):

select
    access0_.id as id127_,
    access0_.libelle as libelle127_,
    access0_.name as name127_ 
from
    Access access0_ 
where
    ? in (
        select
            role2_.id 
        from
            Access_ROLES listrole1_,
            ROLES role2_ 
        where
            access0_.id=listrole1_.Access_id 
            and listrole1_.listRole_id=role2_.id
    )

Everything seems correct (tested with Hibernate Core 3.3.0.SP1, Hibernate Annotations 3.4.0.GA, Hibernate EM 3.4.0.GA)

What version of Hibernate (Core, Annotations, EntityManager) are you using exactly? What error do you get exactly? Can you show how you invoke the query?

灰色世界里的红玫瑰 2024-09-22 06:36:19

我的两个类之间的链接@ManyToMany没有以正确的方式编写,在项目构建期间,在MySQL中创建了2个表(“access_role”为我的链接@ManyToMany在“access”类中,role_access为我的链接@ “角色”类中的ManyToMany)

因此,为了纠正这个问题,我进行了这样的修改

public class Access implements Serializable {
    // ...
    @ManyToMany(mappedBy="listAccess")
    private List<Role> listRole;
   // ...
}

public class Role implements Serializable {
    // ...
    @ManyToMany
        @JoinTable(name = "access_role",
            joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "access_id", referencedColumnName = "id"))
    private List<Access> listAccess;
    // ...
}

My link @ManyToMany between my two classes isn't write in the right way, during the project's building, 2 Tables has created in MySQL ("access_role" for my link @ManyToMany in the 'access' class, and role_access for my link @ManyToMany in the 'role' class)

So, to correct this, I modified like this

public class Access implements Serializable {
    // ...
    @ManyToMany(mappedBy="listAccess")
    private List<Role> listRole;
   // ...
}

public class Role implements Serializable {
    // ...
    @ManyToMany
        @JoinTable(name = "access_role",
            joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "access_id", referencedColumnName = "id"))
    private List<Access> listAccess;
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文