save the transient instance before flushing

Posted by eugenn on Stack Overflow See other posts from Stack Overflow or by eugenn
Published on 2011-02-08T14:21:34Z Indexed on 2011/02/08 15:25 UTC
Read the original article Hit count: 277

Filed under:

Exception: object references an unsaved transient instance - save the transient instance before flushing: Child

How to reproduce issue: 1. Hibernate is load the entity "Parent". The property "child" is null 2. The "Parent" is rendered on the screen and after that the "child" property is auto instantiated. So I have the following graph:

Parent.child != null
Parent.child.childId = null
Parent.child.childKey = ""
Parent.child.childName = ""

Question: How I could to force the Hibernate to ignore updating or inserting Child entity WHEN childId = null? If childId != null I would like just create relation.

<hibernate-mapping>
  <class name="com.test.Parent"
    entity-name="ParentObject" table="parent"
    dynamic-insert="false" dynamic-update="true" optimistic-lock="version">
    <id name="rowId" type="long">
      <column name="RowID" />
      <generator class="native" />
    </id>
    <version name="versionSequence" type="integer"
      unsaved-value="null" generated="never" insert="false">
      <column name="VersionSequence" />
    </version>
    <many-to-one name="child" entity-name="Child" fetch="select"
      optimistic-lock="true" embed-xml="false" update="true" insert="false">
      <column name="ChildID" />
    </many-to-one>
    <property name="dateCreated" type="timestamp">
      <column name="DateCreated" length="0" />
    </property>
    <property name="dateUpdated" type="timestamp" update="false">
      <column name="DateUpdated" length="0" />
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="com.Child"
    entity-name="Child" table="Child" dynamic-insert="false"
    dynamic-update="true" optimistic-lock="version">
    <id name="childId" type="long" >
      <column name="ChildID" />
      <generator class="native" />
    </id>
    <version name="versionSequence" type="integer" insert="false"
      generated="never" >
      <column name="VersionSequence" />
    </version>
    <property name="childKey" type="string" >
      <column name="ChildKey" length="20" />
    </property>
    <property name="childName" type="string" >
      <column name="ChildName" length="30" />
    </property>
    <property name="childNumber" type="string" >
      <column name="ChildNumber" />
    </property>
    <property name="dateCreated" type="timestamp">
      <column name="DateCreated" />
    </property>
    <property name="dateUpdated" type="timestamp" update="false">
      <column name="DateUpdated" />
    </property>
  </class>
</hibernate-mapping>

© Stack Overflow or respective owner

Related posts about hibernate