Skip to content

Problem with Bean Mapping Style due to Type Erasure #457

Closed
@ianagius

Description

@ianagius

When using Bean MappingStyle (using @id on getters), an issue is being encountered with the use of the @id annotation on an extended generic method. This results in an ENTITY_WITHOUT_ID Exception ("Class '%s' mapped as Entity has no Id property. Use @id annotation to mark unique and not-null Entity identifier") being thrown.

This can be easily replicated when we have an abstract Parent class that defines a getter method which is overridden by a child class getter annotated with @id. Example:

public abstract class AbstractEntity<ID extends Serializable> {
    public abstract ID getId();
}

and child:

public class Entity extends AbstractEntity<Long> {
    private Long id;

    public Entity(Long id) {
        this.id = id;
    }

    @Id
    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

Due to type erasure and generated bridge method (https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html), the following methods will be generated:

public java.io.Serializable org.javers.core.model.Entity.getId(); // bridge method
public java.lang.long org.javers.core.model.Entity.getId(); // our method with @Id annotation
public java.lang.long org.javers.core.model.Entity.setId();

In JaversMethodFactory.getAllMethods(), sometimes this method processes the 'bridge' method first, before our method, and this results in our method to be skipped since the logic filters inheritance duplicates. A suggested solution is to skip the bridge method, thus our method can be added.

PR with failing test case and proposed solution will be pushed soon.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions