简体   繁体   中英

Insert into join table in many-to-many relationship

Problem #1:

I have three tables; User , UserRole , and UserRoleRelationships (join table). The UserRole table contain all the user roles which I want to associate with the user. 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: 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:

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 :

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

Problem #2:

When I update the user, hibernate delete the relations in the join table. How can I avoid this?

How can I get hibernate to save the new user with an existing user role in the join table?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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