简体   繁体   中英

save the transient instance before flushing

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>

Use 'cascade' settings. Don't know how to do that with xml, but I'm sure you'll find it in the reference

As far as I understand your question, your presentation layer adds an unnecessary child to the Parent object being displayed, and you want Hibernate to ignore it.

If so, it looks like a responsibility of the presentation layer to get rid of that child. So, it would be better to "sanitize" the Parent by removing that child inside a presentation layer before passing it back to Hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM