title: JPA persist many to many
body: <p>I have a pretty standard scenario whereby I have a table of Users with user_id as the PK and a table of Roles with role_id as the PK. The two tables are related via a many to many relationship (ie. Users can have many roles and a role can be applied to many users) and subsequently I have a joining table called users_has_roles. The only two columns in users_has_roles are users_user_id and roles_role_id.</p>
  
  <p>I have generated the entity classes (see below) and I have no problem persisting data to the users and roles tables but I have failed miserably persist anything to the users_has_roles joining table so currently none of my users are being assigned a role. Before I go crazy could somebody put me out of my misery and show me how I should go about adding a users_user_id with a corresponding roles_role_id to the users_has_roles table so my users can have roles?</p>
  
  <p>My Users.java entity class:</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
  @NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
  @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
  @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password")})
  public class Users implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 60)
  @Column(name = "user_id")
  private String userId;
  @Basic(optional = false)
  @NotNull
  @Pattern(regexp="[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")
  @Size(min = 1, max = 45)
  @Column(name = "username")
  private String username;
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 120)
  @Column(name = "password")
  private String password;
  @JoinTable(name = "users_has_roles", joinColumns = {
      @JoinColumn(name = "users_user_id", referencedColumnName = "user_id")}, inverseJoinColumns = {
      @JoinColumn(name = "roles_role_id", referencedColumnName = "role_id")})
  @ManyToMany
  private Collection&lt;Roles&gt; rolesCollection;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "usersUserId")
  private Collection&lt;UserAccount&gt; userAccountCollection;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "usersUserId")
  private Collection&lt;UserDetails&gt; userDetailsCollection;
  
  ...
  
  All the getter and setter methods etc.
  </code></pre>
  
  <p>My Roles.java entity class:</p>
  
  <pre><code>@Entity
  @Table(name = "roles")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "Roles.findAll", query = "SELECT r FROM Roles r"),
  @NamedQuery(name = "Roles.findByRoleId", query = "SELECT r FROM Roles r WHERE r.roleId = :roleId"),
  @NamedQuery(name = "Roles.findByRoleName", query = "SELECT r FROM Roles r WHERE r.roleName = :roleName"),
  @NamedQuery(name = "Roles.findByRolePermission", query = "SELECT r FROM Roles r WHERE r.rolePermission = :rolePermission"),
  @NamedQuery(name = "Roles.findByRoleDescription", query = "SELECT r FROM Roles r WHERE r.roleDescription = :roleDescription")})
  public class Roles implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 60)
  @Column(name = "role_id")
  private String roleId;
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 45)
  @Column(name = "role_name")
  private String roleName;
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 45)
  @Column(name = "role_permission")
  private String rolePermission;
  @Size(max = 45)
  @Column(name = "role_description")
  private String roleDescription;
  @ManyToMany(mappedBy = "rolesCollection")
  private Collection&lt;Users&gt; usersCollection;
  
  ...
  
  All the getter and setter methods etc.
  </code></pre>
  
  <p>Thanks</p>
  
  <p>---- UPDATE ----</p>
  
  <pre><code>// New Users
  Users currentUser = new Users();
  currentUser.setUserId(userId);
  currentUser.setUsername(email);
  currentUser.setPassword(password);
  getUsersFacade().create(currentUser);
  </code></pre>