2017-03-14 16 views
1

Hibernateによるデータベーススキームの作成について質問があります。Hibernateの多対多と多対1の同時マッピング

私たちのプロジェクトでは、ユーザーとグループがあります。グループには正確に1人の所有者がいて、複数のメンバーを持つことができます。ユーザーは、複数のグループの所有者およびメンバーになることができます。

我々は、次の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

usergroupが外部キーとしてuseridを持っている理由を誰かが説明できますか?

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 
} 

答えて

0

OK:完全を期すために

(あなたが画像で見ることができるように)、ここではユーザーとのUserGroupのためのコードです。問題は多対一マッピングにあります。 userIdが現在のユーザーのIDと等しいすべてのグループにownedUserGroupsを設定しようとしていますか?ただし、owner_UserIdがユーザーのIDと等しいすべてのグループを探す必要があります。基本的には、userIdをowner_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> 
関連する問題