BytecodeEnhancement's AssociationManagement breaks with entity containing two ToOne relations with the same mappedBy value.

Description

Bytecode Enhancement fails at the Association Management step when there is more than one ToOne relationship with the same mappedBy value, as follows:

1 2 3 4 5 6 7 8 9 10 @Entity public class Team { @Id private Integer id; @OneToOne(mappedBy="team") private Member member; @OneToOne(mappedBy="team") private Activity activity;

At build time Bytecode Enhancement I receive the following error message:

Error Message

[ERROR] Failed to execute goal org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:5.0.7.Final:enhance (default) on project test-case-template-hibernate-orm5: Unable to enhance class: com.test.entities.Activity: Unable to enhance persistent attribute [com.test.entities.Activity:team]: Could not enhance entity class [com.test.entities.Activity] to add field writer method [$$_hibernate_write_team]: [source error] $$_hibernate_write_member(com.test.entities.Activity) not found in com.test.entities.Team -> [Help 1]

The key part of the error message is: "$$_hibernate_write_member(com.test.entities.Activity)".
It looks for a badly formed method description, the method should receive a com.test.entities.Member instance.

This happens because the association between entities is considering only the mappedBy value, it should also consider the Typing of the variable.

I partially fixed the problem overriding the method getMappedByFromTargetEntity in the org.hibernate.bytecode.enhance.internal.PersistentAttributesHelper class by complementing this test:

1 if ( context.isPersistentField( f ) && getMappedByFromAnnotation( f ).equals( persistentField.getName() ) ) {

with this:

1 if ( context.isPersistentField( f ) && getMappedByFromAnnotation( f ).equals( persistentField.getName() ) && f.getFieldInfo2().getDescriptor().equals(Descriptor.of(persistentField.getDeclaringClass().getName()))) {

This is not a proper solution, just a starting point, it doesn't take into account type hierarchy for example.

Test Case attached, it uses hibernate-enhance-maven-plugin for testing.
It fails at build test step.

The build succeeds when setting "enableAssociationManagement" to false.

Environment

None

Status

Assignee

Luis Barreiro

Reporter

Rodrigo Lourenço

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.0.8
5.1.0

Priority

Major