简体   繁体   English

Hibernate(使用Spring)多对多查询

[英]Hibernate (using Spring) query with many-to-many

I have user and a userRole table with a many-to-many relationship.我有一个用户和一个具有多对多关系的 userRole 表。 I have the following Hibernate mapping:我有以下 Hibernate 映射:

<hibernate-mapping>
<class name="domain.User" schema="dbo" table="Users">
<id name="userId" type="int">
  <column name="UserId"/>
  <generator class="native"/>
</id>
<property name="username" type="string">
  <column name="Username" not-null="true"/>
</property>
<property name="password" type="string">
  <column name="Password" not-null="true"/>
</property>
<set cascade="all" inverse="true" name="userRole" table="UserRoleRelationships">
  <key>
    <column name="UserId"/>
  </key>
  <many-to-many class="domain.UserRole" column="RoleId" />
</set>
</class>
</hibernate-mapping>

and:和:

<hibernate-mapping>
<class name="domain.UserRole" schema="dbo" table="UserRoles">
<id name="roleId" type="int">
  <column name="RoleId"/>
  <generator class="native"/>
</id>
<property name="role" type="string">
  <column name="Role" not-null="true"/>
</property>
</class>
</hibernate-mapping>

My beans look like:我的豆子看起来像:

public class User {

private Integer userId;
private String username;
private String password;
private Set<UserRole> userRole;

// getters and setters
}

public class UserRole {

private Integer roleId;
private String role;
private User user;

// getters and setters
}

The query for selecting a user by its username looks like:通过用户名选择用户的查询如下所示:

public List<User> getWithUsername(String username){
    return getHibernateTemplate().find("from User as u "
            + "inner join fetch u.userRole "
            + "where u.username = '" + username + "'" );
}

The problem is that when I try to print this out with the following loop(s):问题是当我尝试使用以下循环打印出来时:

for (User u : list){
            System.out.println(u.getUsername());

            for (UserRole ur : u.getUserRole()){
                System.out.println(ur.getRole());
            }  
        }

It prints it double up:它把它加倍打印:

Username: jorgen
User role: User
User role: Admin
Username: jorgen
User role: User
User role: Admin

What am I doing wrong?我究竟做错了什么? Please help:)请帮忙:)

You're not doing anything wrong.你没有做错什么。 Hibernate just does this, and it's related to the way it builds queries. Hibernate 就是这样做的,这与它构建查询的方式有关。 If you would do the same thing in SQL, you'd also have two rows.如果你在 SQL 中做同样的事情,你也会有两行。

There are two general ways of solving this:解决这个问题的一般方法有两种:

  • You can use the DistinctRootEntityProjection but as far as I know, that only works with criteria您可以使用DistinctRootEntityProjection但据我所知,它仅适用于标准
  • You can filter out the duplicates yourself (I suggest you encapsulate that logic in the class calling your hibernate).您可以自己过滤掉重复项(我建议您将该逻辑封装在调用您休眠的 class 中)。 The easiest way is to just throw your items in a set and get them out again.最简单的方法是将您的物品放在一组中并再次取出。

I found a solution doing it with criteria:我找到了一个符合标准的解决方案:

public List<User> getWithUsername(String username){

    Criteria criteria = getSession().createCriteria(IWUser.class)
    .add(Restrictions.like("username", username))
    .setFetchMode("userRole", FetchMode.EAGER)
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    return criteria.list();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM