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

时间:2017-03-14 18:01:42

标签: java postgresql hibernate orm hibernate-mapping

我对Hibernate创建数据库方案有疑问。

在我们的项目中,我们有用户和组。组只有一个所有者,可以有多个成员。用户可以是多个组的所有者和成员。

我们提出了以下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>

Hibernate为我们创建的数据库方案看起来如下: Database scheme created by Hibernate

有人可以解释为什么usergroupuserid作为外键吗? (如图所示)

为了完整起见,这里是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
}

1 个答案:

答案 0 :(得分:0)

确定。问题在于您的多对一映射。您正在尝试将ownUserGroups设置为userId等于当前用户的id的所有组。但是,您需要查找owner_UserId等于用户ID的所有组。基本上你只需要用owner_UserId替换userId。您的最终映射文件应如下所示:

<?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>