[英]How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error
[英]How can I fix “object references an unsaved transient instance - save the transient instance before flushing” on a large project?
我已经在Ubuntu 14.04和Tomcat 6上下载并安装了Perseus项目的源代码( 开放源代码 )。该项目多年来没有被修改,因此它必须是休眠中的一些新功能。
我在很多线程上都看到了解决方案,但是它们解决了调试代码的问题,而不是移植一个肯定可以“一次”运行的项目。 自2008年至2011年以来,我正在使用的代码没有经过修改。 (我确定他们现在正在“ Mandriva Linux 2010.2”上运行它。)因此,我需要能够解决此问题的大规模方法-搜索/替换方法或配置文件更改。 我从未使用过休眠状态,所以我不理解所发布答案的全部含义。
谢谢。
另外,文件不使用@注释语法,但是映射似乎是通过XML文件完成的。 此时,如果我知道要更改什么,我将不介意仅更新所有这些文件。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="perseus.document.dao">
<class name="HibernateChunkDAO$ChunkRow" table="hib_chunks" batch-size="10">
<id name="id" type="int" column="id" unsaved-value="null">
<generator class="native" />
</id>
<property name="documentID" column="document_id" type="string"
length="50" index="doc_idx doc_type_idx doc_type_value_idx
doc_cid_idx doc_so_eo_idx doc_so_eo_type_idx doc_dq_idx
doc_so_eo_type_val_idx" />
<property name="elementName" column="element" type="string"
length="20" />
<property name="type" column="type" type="string" length="30"
index="doc_type_idx doc_type_value_idx doc_so_eo_type_idx
doc_so_eo_type_val_idx" />
<property name="value" column="value" type="string" length="250"
index="doc_type_value_idx doc_so_eo_type_val_idx" />
<property name="position" column="position" type="int" />
<property name="absolutePosition" column="abs_position" type="int" />
<property name="chunkID" column="chunk_id" type="string"
length="255" index="doc_cid_idx" />
<property name="openTags" column="open_tags" type="text" />
<property name="closeTags" column="close_tags" type="text" />
<property name="startOffset" column="start_offset" type="int"
index="doc_so_eo_idx doc_so_eo_type_idx doc_so_eo_type_val_idx" />
<property name="endOffset" column="end_offset" type="int"
index="doc_so_eo_idx doc_so_eo_type_idx doc_so_eo_type_val_idx" />
<property name="displayQuery" column="display_query" type="string"
length="100" index="doc_dq_idx" />
<property name="head" column="head" type="text" />
<property name="headLanguage" column="head_lang" type="string"
length="10" />
<property name="hasCustomHead" column="custom_head" type="boolean" />
<set name="frequencies" inverse="true" cascade="all-delete-orphan"
lazy="true" batch-size="30">
<key column="chunk_id" on-delete="cascade" />
<one-to-many class="perseus.ie.freq.Frequency" />
</set>
<many-to-one name="lemma" column="lemma_id" cascade="all" lazy="false" />
<!--
<list name="senses" inverse="false" cascade="all-delete-orphan"
lazy="false" batch-size="30">
<key column="chunk_id" />
<list-index column="position" />
<one-to-many class="perseus.voting.Sense" />
</list>
-->
</class>
</hibernate-mapping>
发生这种情况是由于以下原因:
您已经在实体中定义了一个尚未持久化的关联,并且在持久化该关联之前,您尝试刷新Session。例如,您有一个具有临时实例集合的collection关联,因此您必须持久化此collection首先在冲洗之前。
要解决此问题,您有2个选择:
通过调用Session.save()方法,将集合手动保存在代码中。
如果要在Hibernate XML文件中使用标签或要使用注释,请在您的集合关联上使用cascade =“ all”(对于xml)或cascade = CascadeType.ALL(对于注释)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.