简体   繁体   English

以多对多关系插入连接表

[英]Insert into join table in many-to-many relationship

Problem #1:问题 1:

I have three tables;我有三张桌子; User , UserRole , and UserRoleRelationships (join table). UserUserRoleUserRoleRelationships (连接表)。 The UserRole table contain all the user roles which I want to associate with the user. UserRole表包含我想与用户关联的所有用户角色。 When I insert a new user I want to add a new user and add a new association in the join table.当我插入一个新用户时,我想添加一个新用户并在连接表中添加一个新关联。 Now, when I'm running the query for inserting a new user:现在,当我运行插入新用户的查询时:

IWUser iwUser = new IWUser();

    iwUser.setUsername("username");
    iwUser.setFullName("fullName");
    iwUser.setEmail("email");
    iwUser.setPassword("password");
    iwUser.setPrivatephone("55555");
    iwUser.setWorkphone("777");


    Set<IWUserRole> roleList = new HashSet<IWUserRole>();
    IWUserRole iwUserRole = new IWUserRole();
    iwUserRole.setRole("ROLE_USER");
    roleList.add(iwUserRole);

    iwUser.setUserRole(roleList);
    iwUserManagementService.saveOrUpdate(iwUser);

hibernate is running the following queries: hibernate 正在运行以下查询:

Hibernate: insert into dbo.Users (Username, Password, Email, Workphone, Privatephone,  FullName) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into dbo.UserRoles (Role) values (?)
Hibernate: insert into UserRoleRelationships (UserId, RoleId) values (?, ?)

My hibernate mapping looks like:我的 hibernate 映射如下所示:

IWUser.hbm.xml : IWUser.hbm.xml

<hibernate-mapping>
<class name="domain.IWUser" 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>
<property name="email" type="string">
  <column name="Email" not-null="false"/>
</property>
<property name="workphone" type="string">
  <column name="Workphone" not-null="false"/>
</property>
<property name="privatephone" type="string">
  <column name="Privatephone" not-null="false"/>
</property>
<property name="fullName" type="string">
  <column name="FullName" not-null="false"/>
</property>
<set cascade="all" inverse="false" name="userRole" table="UserRoleRelationships" lazy="true" >
  <key>
    <column name="UserId"/>
  </key>
  <many-to-many class="domain.IWUserRole" column="RoleId"/>
</set>
</class>
</hibernate-mapping>

IWUserRole.hbm.xml : IWUserRole.hbm.xml

<hibernate-mapping>
 <class name="domain.IWUserRole" 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>
<set cascade="all" inverse="false" name="user" table="UserRoleRelationships" lazy="true">
  <key>
    <column name="RoleId"/>
  </key>
  <many-to-many class="domain.IWUser" column="UserId"/>
 </set>
 </class>
</hibernate-mapping>

How can I get hibernate to save the new user with an existing user role in the join table?如何获取 hibernate 以在连接表中保存具有现有用户角色的新用户?

Problem #2:问题2:

When I update the user, hibernate delete the relations in the join table.当我更新用户时,hibernate 删除连接表中的关系。 How can I avoid this?我怎样才能避免这种情况?

How can I get hibernate to save the new user with an existing user role in the join table?如何获取 hibernate 以在连接表中保存具有现有用户角色的新用户?

Retrieve the user role entity and put that into the list instead of always creating a new one.检索用户角色实体并将其放入列表中,而不是总是创建一个新的。

I mean this part:我的意思是这部分:

IWUserRole iwUserRole = new IWUserRole();
iwUserRole.setRole("ROLE_USER");

Instead, you'd issue a query like select r from IWUserRole where r.role = 'ROLE_USER'相反,您会select r from IWUserRole where r.role = 'ROLE_USER'

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

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