1

I have a mapped object that has collection of another mapped objects. Whenever an object in the contained collection is new for the DB, I have an insertion behaviour with consequent update.

My questions are:

- What is the purpose of this secondary update?
- How can I listen to it? 

Adding a PreUpdateEventListener does not work.

What I want to do it is to eliminate this secondary update to the same values and get it done by single insertion only.

I've found such a question here: https://forum.hibernate.org/viewtopic.php?f=1&t=1011641 but there is no answer.

Also I've found there is a flag "update", it seems does the trick but is that a reason of described behaviour?

Mapping:

<class>
 ...
<map name="attributes" table="DEF"
     cascade="all-delete-orphan" lazy="false" batch-size="5">
    <key not-null="true">
        <column name="PID"/>
        <column name="CDATE"/>
    </key>
    <map-key column="NAME" type="string" alias="name"/>
    <one-to-many entity-name="DocumentAttributes"/>
</map>
  ...
<class>

 <class name="com.moc.Attribute"
       entity-name="DocumentAttributes" table="DEF"
       dynamic-update="true"
       dynamic-insert="true">..</class>

Hibernate logs:

Hibernate: insert into BDOC (ID, CDATE) values (?, ?) 
// Why this insertion consists all columns despite being dynamic?
Hibernate: insert into DEF (TYPE, VALUE, IS_CHANGED, PID, CDATE, NAME, id) values (?, ?, ?, ?, ?, ?, ?) 
//Why this update happen??
Hibernate: update DEF set PID=?, CDATE=?, NAME=? where id=?   

Is anyone here who can describe such behaviour?

  • https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html says that ```"The not-null and update attributes are useful when mapping a unidirectional one-to-many association."``` Is there a more precise description for this flag. – Raimbek Rakhimbek Mar 25 '19 at 08:14
  • Does this answer your question? [Why hibernate generates insert and update for OneToMany mapping](https://stackoverflow.com/questions/25379046/why-hibernate-generates-insert-and-update-for-onetomany-mapping) – Jens Schauder Feb 03 '21 at 08:06

0 Answers0