以下源代码在大多数情况下都能正常工作:
有时我在更新AClass后看到了另一种行为:
Hibernate检查此列表中的最大数量(18)并决定创建19个项目的列表,1到9个ID之间的所有项目都为空。结果我在setBclass方法中有NullPointer。
一些补充说明:
为什么排名值发生了变化?
public class AClass implements Cloneable {
private int id = -1;
private List<BClass> bclasses;
public void setBclass(List<BClass> 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;
}
映射:
<class name="AClass" table="aclass" >
<cache usage="read-write"/>
<id column="id" name="id" type="int" unsaved-value="-1">
<generator class ="increment"/>
</id>
<list name="bclasses" table="bclass" cascade="all">
<cache usage="nonstrict-read-write"/>
<key column="aclass_id" />
<list-index column="rank"/>
<one-to-many class="bclass" />
</list>
</class>
<class name="BClass" table="bclass">
<cache usage="read-write"/>
<id column="id" name="id" type="int" unsaved-value="-1">
<generator class ="increment"/>
</id>
<property column="rank" name="rank" type="byte" />
</class>