Working with Jackson and Hibernate that both support polymorphism, i would like to find a nice Java mapping library that will help me to separate my different layers (mainly DTO/Entities) and that will also support polymorphism.
Orika seems to support polymorphism (but not without declarations of Types). https://orika-mapper.github.io/orika-docs/
I didn't find good examples for the two other contenders Selma and MapStruct. http://www.selma-java.org/ http://mapstruct.org/
Have anyone experienced with those libraries ?
Update 1: One of the test case.
public class PolymorphicMapperTest {
public abstract class Animal{
private String fooAnimal;
public Animal(String fooAnimal) {
this.fooAnimal = fooAnimal;
}
public String getFooAnimal() {
return fooAnimal;
}
}
public class Cat extends Animal {
private String fooCat;
public Cat(String fooAnimal, String fooCat) {
super(fooAnimal);
this.fooCat = fooCat;
}
}
public class Snake extends Animal {
private String fooSnake;
public Snake(String fooAnimal, String fooSnake) {
super(fooAnimal);
this.fooSnake = fooSnake;
}
public String getFooSnake() {
return fooSnake;
}
}
public abstract class AnimalDTO{
public String fooAnimal;
}
public class CatDTO extends AnimalDTO {
public String fooCat;
}
public class SnakeDTO extends AnimalDTO {
public String fooSnake;
}
@Test
public void testMapping() {
Mapper mapper = MyAwesomeMapper();
mapper.declare(Animal.class,AnimalDTO.class);
SnakeDTO snakeDTO = new SnakeDTO();
snakeDTO.fooAnimal = "Snake";
snakeDTO.fooSnake = "Anaconda";
//I don't want the mapper to know the input Type because Jackson will hide it to me.
AnimalDTO animalDTOToMap = (AnimalDTO) snakeDTO;
Animal animal1 = mapper.map(animalDTOToMap);
Assert.isTrue(animal1 instanceof Snake);
//And when casted we want to find Animal and Snake fields mapped
CatDTO catDTO = new CatDTO();
catDTO.fooAnimal = "Cat";
catDTO.fooCat = "Persan";
AnimalDTO secondAnimalDTOToMap = (AnimalDTO) catDTO;
Animal animal2 = mapper.map(secondAnimalDTOToMap);
Assert.isTrue(animal2 instanceof Cat);
//And when casted we want to find Animal and Cat fields mapped
}
}
The ticket is open on the MapStruct backlog https://github.com/mapstruct/mapstruct/issues/131