Bi-directional association management does not work with @EmbeddedId as the mapped by attribute in a @OneToMany

Description

Enhancing below classes produces EnhancementException:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import javax.persistence.*; import java.io.Serializable; import java.util.Set; @Entity public class A { @Id private int id; @OneToMany(fetch = FetchType.LAZY, mappedBy = "bKey.a") private Set<B> bSet; } @Entity class B { @EmbeddedId private BKey bKey; } @Embeddable class BKey implements Serializable { @ManyToOne(fetch = FetchType.LAZY, optional = false) private A a; }

Enhancer is trying to lookup the `bKey.a` field on the `B` class and can't find it, because it's not a field, but rather a field path expression:

https://github.com/hibernate/hibernate-orm/blob/b7b8f44298191db050f12819ca0594a30124df75/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/BiDirectionalAssociationHandler.java#L70

This is a blocker for using bytecode enhancement association management feature on a Maven/Gradle project with at least one entity using `@EmbeddedId` as there's no configuration point for excluding certain classes from enhancement in Maven/Gradle plugins.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 [INFO] --- hibernate-enhance-maven-plugin:5.4.2.Final:enhance (default) @ test-case-template-hibernate-orm5 --- [INFO] Starting Hibernate enhancement for classes on .../target/classes мая 14, 2019 6:49:38 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.4.2.Final} [INFO] Successfully enhanced class [.../target/classes/org/hibernate/bugs/B.class] мая 14, 2019 6:49:38 PM org.hibernate.bytecode.enhance.internal.bytebuddy.BiDirectionalAssociationHandler wrap INFO: Could not find bi-directional association for field [org.hibernate.bugs.BKey#a] [INFO] Successfully enhanced class [.../target/classes/org/hibernate/bugs/BKey.class] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:5.4.2.Final:enhance (default) on project test-case-template-hibernate-orm5: Unable to enhance class: A.class: Failed to enhance class org.hibernate.bugs.A: Could not locate field -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:5.4.2.Final:enhance (default) on project test-case-template-hibernate-orm5: Unable to enhance class: A.class at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288) at org.apache.maven.cli.MavenCli.main(MavenCli.java:199) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) at org.codehaus.classworlds.Launcher.main(Launcher.java:47) Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to enhance class: A.class at org.hibernate.orm.tooling.maven.MavenEnhancePlugin.doEnhancement(MavenEnhancePlugin.java:227) at org.hibernate.orm.tooling.maven.MavenEnhancePlugin.execute(MavenEnhancePlugin.java:152) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207) ... 21 more Caused by: org.hibernate.bytecode.enhance.spi.EnhancementException: Failed to enhance class org.hibernate.bugs.A at org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.enhance(EnhancerImpl.java:142) at org.hibernate.orm.tooling.maven.MavenEnhancePlugin.doEnhancement(MavenEnhancePlugin.java:222) ... 24 more Caused by: java.lang.IllegalStateException: Could not locate field at net.bytebuddy.dynamic.scaffold.FieldLocator$Resolution$Illegal.getField(FieldLocator.java:90) at org.hibernate.bytecode.enhance.internal.bytebuddy.BiDirectionalAssociationHandler.wrap(BiDirectionalAssociationHandler.java:70) at org.hibernate.bytecode.enhance.internal.bytebuddy.PersistentAttributeTransformer.fieldWriter(PersistentAttributeTransformer.java:255) at org.hibernate.bytecode.enhance.internal.bytebuddy.PersistentAttributeTransformer.applyTo(PersistentAttributeTransformer.java:199) at org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.doEnhance(EnhancerImpl.java:292) at org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.lambda$enhance$0(EnhancerImpl.java:136) at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.rewrite(ByteBuddyState.java:141) at org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl.enhance(EnhancerImpl.java:136) ... 25 more

Environment

None

Status

Assignee

Unassigned

Reporter

Dmitry Gusev

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.2

Priority

Major