2016-08-24 74 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的空行正在創建。