简体   繁体   English

Hibernate同时进行多对多和多对一映射

[英]Hibernate simultaneous many-to-many and many-to-one mapping

I have a question about the creation of database schemes by Hibernate. 我有一个关于Hibernate创建数据库方案的问题。

In our project we have users and groups. 在我们的项目中,我们有用户和组。 Groups have exactly one owner and can have multiple members. 组只有一个所有者,可以有多个成员。 A user can be owner and member of multiple groups. 用户可以是多个组的所有者和成员。

We came up with the following Hibernate mapping: 我们提出了以下Hibernate映射:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.User" table="ProductUser">
    <id name="id" column="userId">
        <generator class="native"/>
    </id>
    <property name="email"/>
    <property name="firstName"/>
    <property name="lastName"/>

    <set name="ownedUserGroups" table="UserGroup" inverse="true">
        <key column="userId"/>
        <one-to-many class="model.UserGroup"/>
    </set>

    <set name="userGroups" table="Members" inverse="false" lazy="true" fetch="select">
        <key column="userId"/>
        <many-to-many column="userGroupId" class="model.UserGroup"/>
    </set>
</class>

<class name="model.UserGroup" table="UserGroup">
    <id name="id" column="userGroupId">
        <generator class="native"/>
    </id>
    <property name="name"/>

    <many-to-one name="owner" class="model.User" column="owner_UserId"/>

    <set name="members" table="Members" inverse="true" lazy="true" fetch="select">
        <key column="userGroupId"/>
        <many-to-many column="userId" class="model.User"/>
    </set>

</class>
</hibernate-mapping>

The database scheme Hibernate creates for us looks the following: Database scheme created by Hibernate Hibernate为我们创建的数据库方案看起来如下:Hibernate创建的数据库方案

Can somebody explain why usergroup has the userid as a foreign key? 有人可以解释为什么usergroupuserid作为外键吗? (As you can see in the image) (如图所示)

For the sake of completeness, here is the code for User and UserGroup: 为了完整起见,这里是User和UserGroup的代码:

public class User {
    private int id;
    private String firstName;
    private String lastName;
    private String email;
    private Set<UserGroup> ownedUserGroups;
    private Set<UserGroup> userGroups;

    public User() {
        this.ownedUserGroups = new HashSet<>();
        this.userGroups = new HashSet<>();
    }

    public User(String firstName, String lastName, String email) {
        this();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }
    // getters and setters
}

public class UserGroup {
    private int id;
    private String name;
    private User owner;
    private Set<User> members;

    public UserGroup() {
        this.members = new HashSet<>();
    }

    public UserGroup(String name, User owner, HashSet<User> members) {
        this.name = name;
        this.owner = owner;
        this.members = members;
    }
    // getters and setters
}

Ok. 好。 The problem is with your many-to-one-mapping. 问题在于您的多对一映射。 What your doing is your trying to set ownedUserGroups to all the groups where the userId equals the id of the current user. 您正在尝试将ownUserGroups设置为userId等于当前用户的id的所有组。 However you need to look for all groups where the owner_UserId equals the id of your user. 但是,您需要查找owner_UserId等于用户ID的所有组。 Basically you just need to replace userId with owner_UserId. 基本上你只需要用owner_UserId替换userId。 Your final mapping file should look like this: 您的最终映射文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.User" table="ProductUser">
    <id name="id" column="userId">
        <generator class="native"/>
    </id>
    <property name="email"/>
    <property name="firstName"/>
    <property name="lastName"/>

    <set name="ownedUserGroups" table="UserGroup" inverse="true">
        <key column="owner_userid"/> <!-- CHANGED -->
        <one-to-many class="model.UserGroup"/>
    </set>

    <set name="userGroups" table="Members" inverse="false" lazy="true" fetch="select">
        <key column="userId"/>
        <many-to-many column="userGroupId" class="model.UserGroup"/>
    </set>
</class>

<class name="model.UserGroup" table="UserGroup">
    <id name="id" column="userGroupId">
        <generator class="native"/>
    </id>
    <property name="name"/>

    <many-to-one name="owner" class="model.User" column="owner_UserId"/>

    <set name="members" table="Members" inverse="true" lazy="true" fetch="select">
        <key column="userGroupId"/>
        <many-to-many column="userId" class="model.User"/>
    </set>

</class>
</hibernate-mapping>

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

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