@ManyToMany 并非所有从数据库获取的值

发布于 2025-01-08 04:28:24 字数 481 浏览 1 评论 0原文

我在项目用户中有权限。权限类型存储在数据库中并与用户以多对多的关系链接。

    @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_authority", joinColumns = @JoinColumn(name = "uauth_user"), inverseJoinColumns = @JoinColumn(name = "uauth_authority"))
private Set<Authority> userAuthorities;

但是,当我尝试获取所选用户的所有权限时,我只获取其中之一。 Hibernate 只是获取其中的第一个并将其放入列表中,但忽略用户的所有其他权限。

我已经检查了数据库并存储了这些数据。我还找到了添加一个非 JPA 注释的解决方案。它与 @Fetch(FetchMode.SUBSELECT) 一起使用,但我仍然不明白它有什么问题。

I have in project user which have authorities. Authorities types stored in database and linked with user as many to many relation.

    @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_authority", joinColumns = @JoinColumn(name = "uauth_user"), inverseJoinColumns = @JoinColumn(name = "uauth_authority"))
private Set<Authority> userAuthorities;

But when I try to get all authorities of selected user I obtain just one of them. Hibernate just get first of them and put it to list, but ignore all other authorities of user.

I already check database and it store this data. Also I found solution with adding one not JPA annotations. It works with @Fetch(FetchMode.SUBSELECT) but I still don't understand what is wrong with it.

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

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

发布评论

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

评论(2

╰ゝ天使的微笑 2025-01-15 04:28:24

以下解决方案适用于任何符合 JPA 2.0 的实现。

user 具有主键id

authority 具有主键id

user_authority 具有字段user_idauthority_id

实体类 User

@JoinTable(name = "user_authority", 
    joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},  
    inverseJoinColumns = {@JoinColumn(name = "authority_id", referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.EAGER)
private Set<Authority> authoritySet;

实体类 Authority

@ManyToMany(mappedBy = "authoritySet", fetch = FetchType.EAGER)
private Set<User> userSet;

user_authority 在 JPA 中没有实体表示。

The following solution works on any JPA 2.0 - compliant implementation.

Table user has primary key id.

Table authority has primary key id.

Table user_authority has fields user_id, authority_id.

Entity class User

@JoinTable(name = "user_authority", 
    joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},  
    inverseJoinColumns = {@JoinColumn(name = "authority_id", referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.EAGER)
private Set<Authority> authoritySet;

Entity class Authority

@ManyToMany(mappedBy = "authoritySet", fetch = FetchType.EAGER)
private Set<User> userSet;

The Table user_authority doesn't have an Entity represention in JPA.

笑脸一如从前 2025-01-15 04:28:24

好吧,问题出在不同的地方。

我的错误是,当我用 Hibernate 注释尝试它并且它只是工作时,我开始认为这是一个注释问题,但实际上它是由获取该值的方法引起的。

在重构时,我们在代码中留下了一个错误:

return (T) criteria.setMaxResults(1).uniqueResult();

我们将最大结果计数设置为 1,在我们的例子中,它转换为 SQL,因为

SELECT * FROM user OUTER JOIN user_authority ON usr_id = uauth_user INNER JOIN authority ON uauth_authority = auth_id LIMIT 1

此限制删除了所有权限,只留下第一行的第一个权限。但是当我们指定Hibernate FetchMode为SUBSELECT时。它在两个单独的 SQL 查询中执行此 get。其中主要有限制。

Ok problem was in different place.

My fault was that when I tried it with Hibernate annotation and it is just works I started thinking that this is an annotation problem, but actually it was caused with method of obtaining this values.

While refactoring we leae in code one mistake:

return (T) criteria.setMaxResults(1).uniqueResult();

He we set maximal count of results as 1 and in our case it converted to SQL as

SELECT * FROM user OUTER JOIN user_authority ON usr_id = uauth_user INNER JOIN authority ON uauth_authority = auth_id LIMIT 1

This limit removes all authorities, and leave just first authority from first line. But when we specify Hibernate FetchMode as SUBSELECT. It execute this get in two separate SQL queries. In which just main have limit.

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