Hi all!
Lately I have been working on HHH-6942 JIRA issue. Envers behaves differently when
detached object is updated with new collection and persisted by invoking Session.merge()
or Session.saveOrUpdate()
methods.
SQL statements executed by Session.merge():
14:53:47,031 DEBUG SQL:104 - select setrefcoll0_.id as id1_0_, setrefcoll0_.data as
data1_0_ from SetRefCollEntity setrefcoll0_ where setrefcoll0_.id=?
*14:53:47,078 DEBUG SQL:104 - select collection0_.SetRefCollEntity_id as SetRefCo1_1_1_,
collection0_.collection_id as collection2_1_, strtestent1_.id as id0_0_, strtestent1_.str
as str0_0_ from
SetRefCollEntity_StrTestEntity collection0_ inner join StrTestEntity strtestent1_ on
collection0_.collection_id=strtestent1_.id where collection0_.SetRefCollEntity_id=?*
*14:53:47,125 DEBUG SQL:104 - delete from SetRefCollEntity_StrTestEntity where
SetRefCollEntity_id=? and collection_id=?*
14:53:47,140 DEBUG SQL:104 - insert into REVINFO (REV, REVTSTMP) values (null, ?)
14:53:47,140 DEBUG SQL:104 - insert into SetRefCollEntity_StrTestEntity_AUD (REVTYPE, REV,
SetRefCollEntity_id, collection_id) values (?, ?, ?, ?)
14:53:47,140 DEBUG SQL:104 - insert into SetRefCollEntity_AUD (REVTYPE, data, id, REV)
values (?, ?, ?, ?)
SQL statements executed by Session.saveOrUpdate():
14:54:32,171 DEBUG SQL:104 - select setrefcoll_.id, setrefcoll_.data as data1_ from
SetRefCollEntity setrefcoll_ where setrefcoll_.id=?
*14:54:32,187 DEBUG SQL:104 - delete from SetRefCollEntity_StrTestEntity where
SetRefCollEntity_id=?*
14:54:32,187 DEBUG SQL:104 - insert into SetRefCollEntity_StrTestEntity
(SetRefCollEntity_id, collection_id) values (?, ?)
14:54:32,187 DEBUG SQL:104 - insert into REVINFO (REV, REVTSTMP) values (null, ?)
14:54:32,187 DEBUG SQL:104 - insert into SetRefCollEntity_StrTestEntity_AUD (REVTYPE, REV,
SetRefCollEntity_id, collection_id) values (?, ?, ?, ?)
14:54:32,187 DEBUG SQL:104 - insert into SetRefCollEntity_AUD (REVTYPE, data, id, REV)
values (?, ?, ?, ?)
The main difference is that Session.merge() fetches uninitialized collection from database
and deletes a particular record. In this case Envers operates correctly. With
Session.saveOrUpdate() all
associated records are removed and new ones inserted (in general works as expected). When
Session.saveOrUpdate() is called, PreCollectionRemoveEventListener.onPreRemoveCollection()
methods gets
executed with null collection attribute of PreCollectionRemoveEvent type. Would it be
possible to initialize a collection inside
PreCollectionRemoveEventListener.onPreRemoveCollection()
implementation? If so could you provide me with a sample code? I cannot figure it out.
I've tried using
event.getSession().getPersistenceContext().addUninitializedDetachedCollection() and
event.getSession().initializeCollection() but with no luck.
Regards,
Lukasz