Note that there are some explanatory texts on larger screens.

plurals
  1. POFind and delete an item from a collection
    primarykey
    data
    text
    <p>I have the following problem with a method of JPA. I can not delete an item from a collection. Actually, the method only works if it is not the last element inserted. Where I'm wrong?</p> <p>This is the class of my model:</p> <pre><code>@Entity public class JobOffer { @SequenceGenerator(name="JobOffer_Gen", sequenceName="JobOffer_Seq", initialValue=1, allocationSize=1) @Id @GeneratedValue(generator="JobOffer_Gen") @Column(name="ID_JOBOFFER") private Long id; @Column private String title; ... @OneToMany @JoinTable(joinColumns = @JoinColumn(name = "JOBOFFER_IDFK"), inverseJoinColumns = @JoinColumn(name = "LANGUAGE_IDFK")) private Collection&lt;Language&gt; languages = new HashSet&lt;Language&gt;(); ... } </code></pre> <p>This is my method in JPA:</p> <pre><code>@Override @Transactional public void deleteLanguage(JobOffer joboffer, Long idLingua) throws BusinessException { for(Language l : joboffer.getLanguages()){ if (l.getId() == idLingua){ joboffer.getLanguages().remove(l); } } em.merge(joboffer); } </code></pre> <p>What is the correct way to search for an item in a collection and delete in JPA?</p> <p>This is the error that I get from the console:</p> <pre><code>21-ott-2013 18.22.27 org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [jobbook] in context with path [/jobbook] threw exception [Request processing failed; nested exception is java.util.ConcurrentModificationException] with root cause java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) at java.util.AbstractList$Itr.next(AbstractList.java:343) at org.eclipse.persistence.indirection.IndirectList$1.next(IndirectList.java:571) at it.univaq.mwt.j2ee.jobbook.business.impl.JPAJobOfferService.deleteLanguage(JPAJobOfferService.java:95) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) </code></pre> <p>and then continues</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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