开发者

INSERT instead of UPDATE with Hibernate

开发者 https://www.devze.com 2023-03-25 20:39 出处:网络
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:

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 cl开发者_如何学Pythonass="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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消