Allow initialization during flush

Description

For more details see the attached testcase. I'm sorry, but in the short of time i only got a testcase for jboss-server 4.2. Please deploy the server.ear from /release-directory and then call the /src/client/TestCaseClient.java.

[Summarized]
It seems, that touching a lazy (Persistent-)Collection of at least a m:n relation inside a Hibernate event-listener always raises this error:
org.hibernate.AssertionFailure: collection [n-side] was not processed by flush()

[Explanation]
I have two entities A. and B. Both having a m:n relation between each other. Furthermore there is an PostUpdateListener, which iterates onUpdate of entitiy through all properties of updated entity.

[Testcase]
Both entities are linked with eachother (m:n). If i now do a simple update of a property of entity A --> MyPostUpdateListener will be called, which iterates through every property of the updated entity. In case of this property was a collection (= lazy PersistentCollection of m:n relation), hibernate initializes the collection for further work. I can now run through all objects of the collection, but after all work is done in listener, I get the following exception from postFlush:

Caused by: org.hibernate.AssertionFailure: collection [com.qualitype.testcase.server.ejb.entity.EntityB.entitiesOfA] was not processed by flush()
at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:205)
at org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:333)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:28)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:515)
... 29 more

Attention: EntityB.entitiesOfA is the other-side collection of the m:n relation of the updated EntityA.

We are using hibernate-event listener system for auditing-purposes, so you should understand that touching every (element in the) collection is necessary for audit-purposes.

Seems for me like a serious bug. Need this fixed asap ...

Environment

Windows-XP, Jboss 4.2.1GA, Hibernate 3.2.4SP1, EJB3

Activity

Show:
Steve Ebersole
December 27, 2011, 9:55 PM

I went ahead and implemented this since so many people have requested it. However, be aware that this only affects the ability to initialize the proxies/collections. It does not address further flushes to handle any potential changes to those newly initialized things.

JeffJ
December 27, 2011, 10:39 PM

Big thanks Steve. This is incredibly helpful. I've got lots of listeners that need this, and some hacky workarounds I will be able to remove when 4.0.1 ships.

Steve Ebersole
December 27, 2011, 10:42 PM

Well as long as you realize that this can potentially lead to "missed flushes" you will be fine. Also, be aware that it leads to a larger-than-necessary persistence context ("first level cache").

Steve Ebersole
January 11, 2012, 8:11 PM

Prepping 4.0.1

ranjeet singh
July 6, 2016, 6:41 AM

Please some one tell me class name of context object .
context.convertAnother(item);

Fixed

Assignee

Steve Ebersole

Reporter

SS

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major