Skip to content

Implement type hints for classes #27

@christian-lechner

Description

@christian-lechner

Store type hints in documents when they are persisted to the DB (if the property type and actual type do not match).

Example:

public interface Person {
    String getName();
}

public class Employee implements Person {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }
}

public class Company {
    @Key
    private String key;

    private Person manager;

    public String getKey() {
        return key;
    }

    public Person getManager() {
        return manager;
    }

    public void setManager(Person manager) {
        this.manager = manager;
    }
}

Employee manager = new Employee();
manager.setName("Jon Doe");
Company comp = new Company();
comp.setManager(manager);
arangoOperations.insert(comp);
arangoOps.find(comp.getKey(), Company.class);

This throws an exception because interface Person can not be instantiated. To fix this problem we need to store an additional property (e.g. _type) with the name of the class:

{
  "manager": {
    "_type": "package.Employee"
    "name": "Jon Doe"
  }
}

First, a org.springframework.data.convert.TypeMapper must be implemented that reads and writes the type information. Then the DefaultArangoConverter needs to write the type hints if the property type and the actual type do not match (here Person and Employee). If an object is read and has a type hint, then this class must be used.

I will have a look at this at the weekend.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions