| Bytecode Enhancement fails at the Association Management step when there is more than one ToOne relationship with the same mappedBy value, as follows:
@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] 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:
if ( context.isPersistentField( f ) && getMappedByFromAnnotation( f ).equals( persistentField.getName() ) ) {
with this:
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. |