Hibernate 5 Bytecode Enhancement Association Management works just in one direction

Activity

Show:
Gail Badner
October 24, 2016, 11:57 PM
Luis Barreiro
October 26, 2016, 2:53 AM

to trigger the association management, at some point there has to be a change in the *ToMany field, even if it's with the same collection. The collection itself is not tracked for changes.

So, instead of:

It's required a call to the setter:

A Susnjar
May 8, 2019, 1:56 PM
Edited

UPDATE: Nevermind. This actually works well, the problem I faced was due to a lack of session flush. It would be cool if the session flush isn't needed here, but it is a good alternative.

This is either same or similar/related to the issue documented at:

https://stackoverflow.com/questions/56030764/hibernate-association-management-from-fresh-new-to-loaded-entities-not-consisten/

What appears to be happening is that the bytecode enhancement will only update the inverse side if that inverse side is already loaded - i.e. it will not lazily load it OR somehow store the action to be done when loaded/updated. This, sort of, invalidates the main advantage of using the bi-directional association management - not having to be concerned with whether there even is another side, what it might be and writing that update code in a way that avoids infinite call loops. It also hides the issue. At this point I can see the following possibilities (not all mutually exclusive - i.e. possibly configurable):

  1. Automatically (implicitly) lazily load the inverse side if it isn't already loaded.

  2. Store the update in alternate structures (dirty tracking) so that it can be applied automatically in the transaction and/or if/when the inverse association is actually accessed.

  3. Fail fast with an exception, do not hide the issue of not being able to do what is, essentially, expected - that the inverse side is updated. This will help discover these issues earlier in the development process.

  4. Give an option / hook to handle this situation.

  5. Not desirable in the long term but a must in the short term - document the issue in "bi-directional association management" bytecode enhancement documentation.

I realize that the issue here has an accepted stackoverflow answer related to the (lack of) unit test bytecode enhancement, but in here I found nothing closer... and this is still "OPEN". What I am reporting may be a separate issue and should be moved out.

Additionally, considerations may need to be made as to what to do when mixing transient and persistent state entities, such as setting an association of a (new) transient entity to point to a (loaded) persistent one and expecting that the persistent's inverse association is updated, when should this happen (when save()/saveOrUpdate()/persist() is called and on which entity - ideally only the one being explicitly modified), etc.

Assignee

Luis Barreiro

Reporter

Amarfii Andrei

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure