jonbooz
jonbooz

Reputation: 37

INSERT instead of UPDATE with Hibernate

I have a class that I'm persisting with Hibernate. It is stored within a bag and uses a sequence generator to generate its ID, as below:

<class name="Parent">
  <id name="id" />
  <bag name="children" access="field">
    <key column="parent_id" not-null="true" />
    <one-to-many class="Child" />
  </bag>
</class>

<class name="Child">
  <id name="id">
    <generator class="sequence">
      <param name="sequence">child_seq</param>
    </generator>
  </id>

  <many-to-one name="status" not-null="true" cascade="none" />
</class>

When the status property of Child is modified, a new Child record should be INSERTed instead of the old one being UPDATEd. However, Hibernate UPDATEs. Is there any way to tell Hibernate that it should INSERT, or will I have to manage it myself outside the bag?

Upvotes: 0

Views: 2164

Answers (1)

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

I believe you have to add a child object manually to the collection. Because with hibernate it's normal practice that each entity is related to a row in a table. So to add a new row you need to add a new child to the collection.

Upvotes: 0

Related Questions