Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate: wrong value in list-index column for one-to-many relationship
    primarykey
    data
    text
    <p>Source code below works fine in most cases:</p> <ul> <li>each AClass has 10 items in the bclasses list</li> <li>all BClass records (10) related to one AClass have rank between 0 and 9</li> </ul> <p>Sometimes I've seen another behavior after updating of AClass:</p> <ul> <li>each AClass has 10 items in the bclasses list</li> <li>all BClass records (10) related to one AClass have rank between 0 and 18, i.e. 0, 10, 11, 12, 13, 14, 15, 16, 17, 18</li> </ul> <p>Hibernate checks the maximum number in this list (18) and makes a decision to create a list of 19 items, all items between 1 and 9 IDs is null. As a result I have NullPointer in the setBclass method.</p> <p>Some additional notes:</p> <ul> <li>There is no method on server side to add/delete BClass into/from the list</li> <li>Server side creates this list and set rank explicitly for each item in the list without gaps</li> </ul> <p>Why rank values were changed ?</p> <pre><code>public class AClass implements Cloneable { private int id = -1; private List&lt;BClass&gt; bclasses; public void setBclass(List&lt;BClass&gt; bclasses) { this.bclasses= bclasses; for (BClass bclass : bclasses) { //do something with bclass //sometimes some items in the list is null } } } public class BClass implements Cloneable { private int id = -1; private byte rank; } </code></pre> <p>Mapping:</p> <pre><code>&lt;class name="AClass" table="aclass" &gt; &lt;cache usage="read-write"/&gt; &lt;id column="id" name="id" type="int" unsaved-value="-1"&gt; &lt;generator class ="increment"/&gt; &lt;/id&gt; &lt;list name="bclasses" table="bclass" cascade="all"&gt; &lt;cache usage="nonstrict-read-write"/&gt; &lt;key column="aclass_id" /&gt; &lt;list-index column="rank"/&gt; &lt;one-to-many class="bclass" /&gt; &lt;/list&gt; &lt;/class&gt; &lt;class name="BClass" table="bclass"&gt; &lt;cache usage="read-write"/&gt; &lt;id column="id" name="id" type="int" unsaved-value="-1"&gt; &lt;generator class ="increment"/&gt; &lt;/id&gt; &lt;property column="rank" name="rank" type="byte" /&gt; &lt;/class&gt; </code></pre>
    singulars
    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.
    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