2016-08-24 69 views
0

我有两个表配置设置其中一个配置的设置的部分(儿童)。休眠获取额外的,NULL,排

我使用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

数据库的实际内容是什么?我想知道如果这可能是不正确设置连接表的结果没有引用一个不存在的设置的外键约束... – Ordous

+0

该数据库恰好包含2行。映射看起来对我来说是正确的。 – algiogia

回答

0

至于回答到this question的问题是使用列表列表指数的:在这种情况下,孩子们的ID必须从0开始,任何缺少指数将使Hibernate来生成一个空值为了它。

在我的情况下,第一个孩子的id = 1,所以0的空行正在创建。