Use of enhancer dirty tracking causes collections in entities to not be persisted

Description

The enhancer isn't dirty tracking collections.

For example, if there is a user that has a Set<Group> groups fields, then:

does not result in the dirty tracker having the "groups" field changed.

Here's a pull request with a test demonstrating the problem:
https://github.com/hibernate/hibernate-orm/pull/1659

Activity

Show:

Former user March 15, 2017 at 12:21 AM

Fixed in 5.1 branch as well.

Vlad Mihalcea December 12, 2016 at 3:47 PM

Applied PR upstream after adding a new test case that validates the fix.

Craig Andrews December 2, 2016 at 5:49 PM

I think a better solution would be to not update the dirty tracker, but instead consider enhanced entities with collections as always being maybe dirty, which matches the behavior of unenhanced entities.

At https://github.com/hibernate/hibernate-orm/blob/5.2.5/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java#L345 I think should be changed to:

I've provided a pull request: https://github.com/hibernate/hibernate-orm/pull/1665

There don't seem to be any applicable tests in Hibernate that cover this behavior, but I tested it and it solves the problem (and makes sense).

Craig Andrews December 2, 2016 at 3:21 AM
Edited

I think the fix would be at https://github.com/hibernate/hibernate-orm/blob/5.2.5/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/AttributeTypeDescriptor.java#L63 - instead of just checking the field is a mapped collection and doing nothing if it is, I think the new value should be checked to see if it's an instance of org.hibernate.collection.spi.PersistentCollection, so I think line 63 should read:

Craig Andrews December 2, 2016 at 2:31 AM

Without enhancement, at https://github.com/hibernate/hibernate-orm/blob/5.2.5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java#L133 mightBeDirty=true. Which allows the collections to be wrapped at https://github.com/hibernate/hibernate-orm/blob/5.2.5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultFlushEntityEventListener.java#L140 and eventually persisted.

The collection wrapping is the important part - mightBeDirty should be true in this case when the enhancer is used to allow that collection wrapping.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created November 30, 2016 at 5:44 PM
Updated March 15, 2017 at 12:34 AM
Resolved March 15, 2017 at 12:21 AM