Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate ManyToMany Java List removal
    primarykey
    data
    text
    <p>I have something like this</p> <pre><code>@Entity public class A implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Basic(optional = false) private Long id; @Basic(optional = false) private String name; @ManyToMany private List&lt;B&gt; bList = new ArrayList&lt;&gt;(0); public A() { } public List&lt;B&gt; getbList() { return bList; } public void setbList(List&lt;B&gt; bList) { this.bList = bList; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p>and </p> <pre><code>@Entity public class B implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Basic(optional = false) private Long id; @Basic(optional = false) private Integer number; @ManyToMany private List&lt;A&gt; aList = new ArrayList&lt;&gt;(0); public B() { } public List&lt;A&gt; getaList() { return aList; } public void setaList(List&lt;A&gt; bList) { this.aList = bList; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } } </code></pre> <p>Also</p> <pre><code>public class C { public C() { } public boolean addAtoB(Session hibernate, A a, B b) throws HibernateException { a.getbList().add(b); b.getaList().add(a); hibernate.saveOrUpdate(a); hibernate.saveOrUpdate(b); A aa = (A) hibernate.get(A.class, a.getId()); B bb = (B) hibernate.get(B.class, b.getId()); if (aa.getbList().contains(b) &amp;&amp; bb.getaList().contains(b)) { return true; } return false; } public boolean removeAfromB(Session hibernate, A a, B b) throws HibernateException { a.getbList().remove(b); b.getaList().remove(a); hibernate.saveOrUpdate(a); hibernate.saveOrUpdate(b); A aa = (A) hibernate.get(A.class, a.getId()); B bb = (B) hibernate.get(B.class, b.getId()); if (!(aa.getbList().contains(b) &amp;&amp; bb.getaList().contains(b))) { return true; } return false; } public boolean addListOfAtoB(Session hibernate, List&lt;A&gt; a, B b) throws HibernateException { boolean test = false; for (int i = 0; i &lt; a.size(); i++) { A aa = a.get(i); addAtoB(hibernate, aa, b); } test = true; return test; } public boolean removeListOfAtoB(Session hibernate, List&lt;A&gt; a, B b) throws HibernateException { boolean test = false; for (int i = 0; i &lt; a.size(); i++) { A aa = a.get(i); removeAfromB(hibernate, aa, b); } test = true; return test; } public Integer halfOfB(Session hibernate, List&lt;B&gt; b) throws Exception { for (int i = 0; i &lt; b.size(); i++) { B bb = b.get(i); List&lt;A&gt; aa = bb.getaList(); if (aa.size() &lt;= 5) { return aa.size(); } else { Integer br = (Integer) hibernate.createCriteria(B.class) .setProjection(Projections.max("number")).uniqueResult() + 1; B bbb = new B(); bbb.setNumber(br); List&lt;A&gt; half = aa.subList(aa.size() / 2, aa.size()); //error removeListOfAtoB(hibernate, half, bb); addListOfAtoB(hibernate, half, bbb); halfOfB(hibernate, b); } } throw new Exception("error in halfOfB"); } } </code></pre> <p>halfOfB is a recursive method that halves list of names into new list until there the list has at least than 5 names from A under a number of B.</p> <p>Before anyone asks why not one to many, becouse I need both entities to have multiple aplication, so ManytoMany is the only option. </p> <p>All methods in junit are green execept test for halfOfB throws java.util.ConcurrentModificationException. According to <a href="http://javaadami.blogspot.com/2007/09/enhanced-for-loop-and.html" rel="nofollow">this</a> if I use indexed for no ConcurrentModificationException should be thrown. Also if i reverse order</p> <pre><code> addListOfAtoB(hibernate, half, bbb); removeListOfAtoB(hibernate, half, bb); </code></pre> <p>I still get same error Any ideas how to make this work?</p>
    singulars
    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.
    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