Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several problems with your code.</p> <p>First one: you don't respect naming conventions. variables should start with a lower-case letter. This makes your code harder to read for seasoned Java programmers.</p> <p>Second one: you loop over your item IDs, and change the value of your <code>ItemprofilPk</code> and <code>Itemprofil</code> objects. But you update always the same objects, and only persist the item after the loop. so, of course, only the last value is persisted. Change to something like this:</p> <pre><code>for (Integer val : getListidItemByItemName()) { AssociationItemProfilPK itemprofilPk = new AssociationItemProfilPK(); AssociationItemProfil itemprofil = new AssociationItemProfil(); itemprofilPk.setItemId(val); itemprofilPk.setProfilId(user.getProfilUserId()); itemprofil.setAssociationItemProfilPK(itemprofilPk); em.persist(itemprofil); } </code></pre> <p>Third one: your mapping is more complex than id needs. You should not have an entity to map the association table. Instead, you should thus have a list of items in the <code>ProfilUser</code> entity, and a list of profilUsers in the <code>Item</code> entity :</p> <pre><code>public class ProfilUser { @ManyToMany @JoinTable( name = "associationProfileUserItem", joinColumns = @JoinColumn(name = "PROFIL_ID"), inverseJoinColumns=@JoinColumn(name="ITEM_ID") private List&lt;Item&gt; items; } public class Item { @ManyToMany(mappedBy = "items") private List&lt;ProfilUser profilUsers; } </code></pre>
 

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