1.在Hibernate中一对多关系映射中:
Operation:
Module module;
Integer turn; ---返回list集合给予次序决定
Module:
List<Operation> operations;
Integer turn;
提供get和set方法就不多说了。
2.映射文件:
Operation.hbm.xml:
<many-to-one name="module" class="Module" column="module_id" lazy="false"/>
Module.hbm.xml:
<list name="operations">
<key column="module_id"/>
<list-index base="1" column="turn"/>
<one-to-many class="Operation"/>
</list>
在某些环境下hql语言为:select distinct m from Module m join fetch m.operations
没问题可以查询出来,
但也会出现bug:查询出来的Module的List集合中每个Module的对象中operations属性集合都增加了很多null值。
所以应将Module.hbm.xml的映射文件中一对多的list关系映射修改:
<bag name="operations">
<key column="module_id"/>
<one-to-many class="Operation"/>
</bag>
可以去除这种情况下的NULL值。