Merge detached entity failed when the instrumented lazy property is initialized

Description

Well, I have an entity that stores a byte[] property as a Blob.

@Lob
@Basic(optional = false, fetch = FetchType.LAZY)
public byte[] getContent() {
return content;
}

The content can be pretty large, so I marked it as lazy and to enable lazy property fetching I am instrumenting the class at build-time using org.hibernate.tool.instrument.javassist.InstrumentTask just as the documentation says.

However, when I call session.merge on this entity, I can get this stacktrace:

java.lang.ClassCastException: org.hibernate.intercept.LazyPropertyInitializer$1 cannot be cast to [B
at org.hibernate.type.AbstractLongBinaryType.toInternalFormat(AbstractLongBinaryType.java:46)
at org.hibernate.type.AbstractBynaryType.isEqual(AbstractBynaryType.java:134)
at org.hibernate.type.NullableType.isEqual(NullableType.java:233)
at org.hibernate.type.MutableType.replace(MutableType.java:60)
at org.hibernate.type.TypeFactory.replace(TypeFactory.java:548)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:495)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:423)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:234)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:859)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:843)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:847)

It seems to work fine on session.get()

I think it is a bug.

Environment

Hibernate-3.5.2, Oracle 10g

Activity

Show:
Gail Badner
July 25, 2015, 1:57 AM

The fix for this jira only works for properties that use "property" access and only when using the "old" byte code instrumentation. IOW, it will not work for the new enhancer in master branch. Another Jira will be added to fix this issue using the new enhancer.

For field access, updates do not get persisted. I will cover that fix HHH-9984..

I suspect that there will still be a problem merging an entity with an initialized lazy embeddable, or a non-lazy embeddable that contains an initialized sub-property. If someone finds that to be the case, please create a new jira.

Luis Barreiro
July 25, 2015, 4:15 AM

For the new bytecode enhancer, check issue for the same use case. Merge won't fail, but changes don't get persisted.

Gail Badner
July 28, 2015, 9:41 PM

Fixed in master and 4.3 branches.

Gail Badner
October 20, 2015, 6:24 PM

Reopening to fix in 4.2.21.

Gail Badner
October 20, 2015, 6:24 PM

Fixed in 4.2 branch.

Assignee

Gail Badner

Reporter

Kyrill Alyoshin

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
Configure