Reputation: 545
I have a function that add into my database (using JPA) a user and his items ,the relationship many-to- many. I have the following tables :
profilUser
item
associationProfileUserItem
on the GUI I enter the login and password of the user and I choose a list of names of items, I have worte a function recuperate id of Item BY their name (to add them to the association). the problem is that the function insert just the last item choosen for the user and ignore the rest!
My code:
public void addProfilUser()
{
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
AssociationItemProfilPK ItemprofilPk=new AssociationItemProfilPK();
AssociationItemProfil Itemprofil=new AssociationItemProfil();
ProfilUser user=new ProfilUser();
user.setLogin(login);
user.setPassword(password);
em.persist(user);
for (Integer val : getListidItemByItemName())
{
ItemprofilPk.setItemId(val);
ItemprofilPk.setProfilId(user.getProfilUserId());
Itemprofil.setAssociationItemProfilPK(ItemprofilPk);
}
em.persist(Itemprofil);
entr.commit();
}
catch (Exception e )
{
System.out.println(e.getMessage());
System.out.println("Failed");
}
finally {
em.close();
emf.close();
}
}
public ArrayList<Integer> getListidItemByItemName()
{
try{
EntityTransaction entityTrans=emm.getTransaction();
entityTrans.begin();
for (String val : listItem)
{
System.out.println("my values"+val);
javax.persistence.Query multipleSelect= em.createQuery("SELECT i.ItemId FROM Item i WHERE i.ItemName IN (:w)" );
multipleSelect.setParameter("w", val);
List ItemId = new LinkedList();
ItemId= multipleSelect.getResultList();
listIdItem = new ArrayList(ItemId);
}
entityTrans.commit();
System.out.println("Id for given item name"+listIdItem);
return listIdItem;
}
catch(Exception e)
{
e.printStackTrace();
}
Upvotes: 0
Views: 261
Reputation: 692231
There are several problems with your code.
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.
Second one: you loop over your item IDs, and change the value of your ItemprofilPk
and Itemprofil
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:
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);
}
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 ProfilUser
entity, and a list of profilUsers in the Item
entity :
public class ProfilUser {
@ManyToMany
@JoinTable(
name = "associationProfileUserItem",
joinColumns = @JoinColumn(name = "PROFIL_ID"),
inverseJoinColumns=@JoinColumn(name="ITEM_ID")
private List<Item> items;
}
public class Item {
@ManyToMany(mappedBy = "items")
private List<ProfilUser profilUsers;
}
Upvotes: 1