Given the following MapStruct mappers, Entities and DTOs I want to know how to solve a circular dependency.
class Foo{
String name;
List<Bar> child;
}
class FooDTO {
String name;
List<BarDTO> child;
}
class Bar{
String name;
Foo parent;
}
class BarDTO{
String name;
FooDTO parent;
}
@Mapper(uses = {BarMapper.class})
interface FooMapper {
// Mapper method when I only need a FooDTO without a reference to its child
@Named("toFooDto")
@Mapping(target = "child", ignore = true)
FooDTO toFooDto(Foo foo);
// Mapper method when I need a FooDTO with a reference to its child (also mapped to DTO)
@Mapping(source = "child", target = "child", qualifiedByName="toBarDto")
FooDTO toFooDtoWithRelationship(Foo foo);
Foo toFooEntity(FooDTO fooDto);
}
@Mapper(uses = {FooMapper.class})
interface BarMapper {
// Mapper method when I only need a BarDTO without a reference to its parent
@Named("toBarDto")
@Mapping(target = "parent", ignore = true)
BarDTO toBarDto(Bar Bar);
// Mapper method when I need a BarDTO with a reference to its parent (also mapped to DTO)
@Mapping(source = "parent", target = "parent", qualifiedByName="toFooDto")
BarDTO toBarDtoWithRelationship(Bar Bar);
Bar toEntity(BarDTO BarDto);
}
When I launch my Spring app I get an error at startup that says the following
APPLICATION FAILED TO START
┌─────┐
| fooMapperImpl (field private com.example.BarMapper com.example.FooMapperImpl.barMapper)
↑ ↓
| barMapperImpl (field private com.example.FooMapper com.example.BarMapperImpl.fooMapper)
└─────┘
Is it possible to make it work?
I would like to have each Entity mapper separated from each other.