Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok first off thanks to Mikko for leading me to the answer. I just wanted to post an answer that might be directly helpful to anybody else that might be in the position I was in. Also this is based on a Eureka moment so it might not be technically correct but this is how I see it.</p> <p>The big issue that I faces was that in MySQL I could see the bridging table as an individual table! (sorry I can't post an image of my EER diagram but I don't seem to have enough privileges at the moment) So I assumed that Java would also see the bridging table as a table! Well it doesn't. That bridging table doesn't really exist in Java as a conventional table it is in fact represented by the opposing tables collection type that you associate with it.</p> <p>The easiest way to see it for me was to completely forget the bridging table and concentrate on the two 'real' tables and associating the data in those. The following code is NOT best practice as I'm simply setting the role_id but it's fine just to show my point.</p> <pre><code>List&lt;Roles&gt; userRoleList = new ArrayList&lt;Roles&gt;(); Users currentUser = new Users(); currentUser.setUserId(userId); currentUser.setUsername(email); currentUser.setPassword(password); Roles userRole = new Roles(); userRole.setRoleId("2"); userRoleList.add(userRole); currentUser.setRolesCollection(userRoleList); getUsersFacade().create(currentUser); </code></pre> <p>Hope that helps anybody else that is struggling with many to many relationships.</p> <p>(NB. I've edited the original question code to use a List instead of a Collection for ease but you can just as well use any other type that fits your needs.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload