I've got a parent and child relation (many actually in the application). I have to call flush() upon every save/update (don't ask why). I've got history triggers that record every update. When the child object is saved and flushed(), it forces the save on the parent and thus executes the parent's history trigger prematurely. What hibernate table settings or trigger adjusments will make the "After" show up in the history table instead of the "Before"? THANK YOU!
Parent:
<hibernate-mapping>
<class lazy="false" name="com.sample.Parent" table="PARENT">
<id name="id" type="long" unsaved-value="null">
<column name="PARENT_ID" not-null="true"/>
<generator />
</id>
<property name="lastName" column="last_name" type="string" />
<set name="children" inverse="true">
<key column="PARENT_ID" />
<one-to-many />
</set>
</class>
</hibernate-mapping>
Child:
<hibernate-mapping>
<class lazy="false" name="com.sample.Child" table="PARENT">
<id name="id" type="long" unsaved-value="null">
<column name="CHILD_ID" not-null="true"/>
<generator />
</id>
<property name="age" column="age" type="int" />
<many-to-one name="parent" column="PARENT_ID" insert="false" update="false"/>
</class>
</hibernate-mapping>
Some java code:
Transaction txn = session.beginTransaction();
Parent p = (Parent) session.load(Parent.class, 58);
// Before DOES show up in the history, so it got flushed by Child save
p.setLastName("Before")
Child c = new Child();
c.setAge(12);
p.getChildren().add(c)
c.setParent(p);
session.save(c);
session.flush()
// After does NOT show up in the history
p.setLastName("After")
session.saveOrUpdate(p);
session.flush();
txn.commit();
SQL Server Trigger Code:
CREATE TRIGGER [parent_update] ON [dbo].[parent] FOR UPDATE AS Begin
Insert into parent_audit_trail(column_value_old,column_value_new,changed_by) select d.last_name,i.last_name,SYSDATETIME() from Deleted d, Inserted i where d.parent_id = i.parent_id and i.first_name != d.first_name
END
CodePudding user response:
