Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6127201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:28:35+00:00 2026-05-23T16:28:35+00:00

Problem #1: I have three tables; User , UserRole , and UserRoleRelationships (join table).

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:28:36+00:00Added an answer on May 23, 2026 at 4:28 pm

    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'

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL Left Join problem. I have three tables which I'm trying
I have three tables: Users, Groups and GroupsUsers User table: id: 1 username: admin
I have three tables: Table User( userid username) Table Key( userid keyid) Table Laptop(
I have three tables, a user-table and two user-type tables. The user-type tables have
I have an issue getting data from three tables, which I want to return
Well here's my problem I have three tables; regions, countries, states. Countries can be
I have three tables being used for this problem: songs, blacklist, and whitelist. The
I have a simple mySQL problem-- I have two tables, one is a user's
For the sake of simplifying my problem, let's say we have three models; user
I have a table with three columns: user varchar, status varchar , rep int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.