Skip to content

Problem with Bean Mapping Style due to Type Erasure #457

Closed

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.

Activity

  1. ianagius commented on Nov 15, 2016

    @ianagius
    ContributorAuthor

    created the following pull request #458

  2. bartoszwalacik commented on Nov 16, 2016

    @bartoszwalacik
    Member

    @ianagius thanks for excelent issue description and the PR. I'll try to merge this evening

  3. added a commit that references this issue on Nov 16, 2016
  4. ianagius commented on Nov 16, 2016

    @ianagius
    ContributorAuthor

    cheers

  5. bartoszwalacik commented on Nov 16, 2016

    @bartoszwalacik
    Member

    merged, your fix will be released tomorrow

  6. bartoszwalacik commented on Nov 17, 2016

    @bartoszwalacik
    Member

    fix released in 2.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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