gpt4 book ai didi

java - Hibernate 同步多对多和多对一映射

转载 作者:行者123 更新时间:2023-11-29 13:49:56 28 4
gpt4 key购买 nike

我有一个关于 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 作为外键吗? (如图所示)

为了完整起见,这里是用户和用户组的代码:

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
}

最佳答案

好的。问题在于您的多对一映射。您所做的是尝试将 ownedUserGroups 设置为 userId 等于当前用户 ID 的所有组。但是,您需要查找 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>

关于java - Hibernate 同步多对多和多对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42793348/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com