title: Database design for User, UserRole - many to many relationship
body: <p>In my project, I have a User entity and a UserRole entity. 
  According to my database design, a user can play multiple roles and a role can be associated with more than one user. The user roles I have in my system is USER and ADMIN.</p>
  
  <p>In need user roles to be stored in the table and they need to be referred when a user is persisted in its relevant table.</p>
  
  <p>However, according to the code I have implemented, records get inserted to the user role table too every time I insert a user to the database. What I need is to insert data into the User table and to the join table.</p>
  
  <p>These are my entities.</p>
  
  <p><strong>User:</strong></p>
  
  <pre><code>@Entity
  @Table(name="user")
  public class User {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", nullable = false, updatable= false)
      private Long id;
  
      @Column(name = "email", nullable = false, unique = true)
      private String email;
  
      @Column(name = "first_name", nullable = false)
      private String firstName;
  
      @Column(name = "last_name", nullable = false)
      private String lastName;
  
      @Column(name = "password_hash", nullable = false)
      private String passwordHash;
  
      @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinTable(name = "user_with_role",
              joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
              inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
      private List&lt;UserRole&gt; roles;
  
  }
  </code></pre>
  
  <p><strong>UserRole:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "userrole")
  public class UserRole {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", nullable = false, updatable= false)
      private long id;
  
      @Column(name = "role_name")
      @NotNull
      @Enumerated(EnumType.STRING)
      private Role roleName;
  
      @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
      private List&lt;User&gt; users;
  }
  </code></pre>
  
  <p>This is how I persist a user to the database.</p>
  
  <pre><code>List&lt;UserRole&gt; roles = new ArrayList&lt;&gt;();
  
  UserRole ur_user = new UserRole();
  ur_user.setRoleName(Role.USER);
  
  UserRole ur_admin = new UserRole();
  ur_user.setRoleName(Role.ADMIN);
  
  roles.add(ur_user);
  roles.add(ur_admin);
  
  newUser.setRoles(roles);
  entityManager.persist(newUser);
  
  entityManager.flush();
  </code></pre>
  
  <p>This mechanism is fine for the situations where the associated should be entered to the database together with the parent entity. (Eg: Contact Number of employees)</p>
  
  <p>In my situation, I really do not understand the way I should write the code in order to not insert to the UserRole table when a user is persisted.</p>
  
  <p>How Should I write the code in my situation. 
  In addition, I would like to know what CascadeType I should use in User entity. I have put <code>CascadeType.ALL</code>, but I know that is not the best type in that place according to the context.</p>