gpt4 book ai didi

java - Hibernate 检索额外的 NULL 行

转载 作者:行者123 更新时间:2023-11-30 07:11:42 24 4
gpt4 key购买 nike

我有两个表配置设置,其中设置配置的一部分(子级)。

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

最佳答案

正如对 this question 的回答问题是使用 listlist-index:在这种情况下,子级的 id 必须从 0 开始,任何缺失的索引都会导致 Hibernate 生成 null它的值(value)。

在我的例子中,第一个子元素的 id = 1,因此正在创建 0 的空行。

关于java - Hibernate 检索额外的 NULL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39125540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com