我有两个表配置和设置,其中设置是配置的部分(子)。< / p>
我使用HibernateDaoSupport.find()来检索配置,结果对象包含3个设置,其中一个是 null 。不是具有空值的对象,而是 null 。
根据日志,Hibernate只发现了两行:
DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [com.xx.Setting#1]
DEBUG [org.hibernate.engine.TwoPhaseLoad] - done materializing entity [com.xx.Setting#1]
DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [com.xx.Setting#2]
DEBUG [org.hibernate.engine.TwoPhaseLoad] - done materializing entity [com.xx.Setting#2]
DEBUG [org.hibernate.engine.loading.CollectionLoadContext] - 1 collections were found in result set for role: com.xx.Configuration.settings
DEBUG [org.hibernate.engine.loading.CollectionLoadContext] - collection fully initialized: [com.xx.Configuration.settings#1]
DEBUG [org.hibernate.engine.loading.CollectionLoadContext] - 1 collections initialized for role: com.xx.Configuration.settings
DEBUG [org.hibernate.loader.Loader] - done loading collection
然后
DEBUG [org.hibernate.pretty.Printer] - com.xx.Setting{dataType=TEXT, name=xx, id=1, value=xx}
DEBUG [org.hibernate.pretty.Printer] - com.xx.Configuration{settings=[null, com.xx.Setting#1, com.xx.Setting#2], id=1, bu=xx}
DEBUG [org.hibernate.pretty.Printer] - com.xx.Setting{dataType=TEXT, name=xx, id=2, value=xx}
正如您所见,它只找到2 设置,但该集合包含3。
编辑:以下是Hibernate映射
<hibernate-mapping>
<class name="com.xx.Configuration"
table="sf_config"
dynamic-insert="false" dynamic-update="false"
mutable="true" optimistic-lock="version"
polymorphism="implicit" select-before-update="false" lazy="false">
<id access="field" name="id">
<generator class="native"/>
</id>
<natural-id>
<property name="bu" access="field"/>
</natural-id>
<list access="field" name="settings" table="sf_setting" lazy="false" cascade="all-delete-orphan">
<key>
<column name="configId"/>
</key>
<list-index column="id"/>
<one-to-many class="com.xx.Setting"/>
</list>
<property name="recordState" access="field"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.xx.Setting"
table="sf_setting"
dynamic-insert="false" dynamic-update="false"
mutable="true" optimistic-lock="version"
polymorphism="implicit" select-before-update="false" lazy="false">
<id access="field" name="id">
<generator class="native"/>
</id>
<property name="name" access="field"/>
<property name="value" access="field"/>
<property name="dataType" access="field"/>
</class>
</hibernate-mapping>
答案 0 :(得分:1)
正如对this question的回答,问题是使用列表和列表索引:在这种情况下,子项的ID必须从0开始任何缺少的索引都会导致Hibernate为它生成一个空值。
在我的情况下,第一个孩子的id = 1,因此正在创建0的空行。