我与循环依赖问题MapStruct挣扎。我一直有一个计算器错误由于循环依赖。为了避免它,我只需要排除列表的属性。我发现这一点:https://github.com/mapstruct/mapstruct/issues/933我深深的看了在互联网上,我一直感到惊讶,我找不到任何充分为例展示与MapStruct双向DTO映射(除使用@Context CycleAvoidingMappingContext
,不工作对我来说)。
[编辑]:我发现了一个workarround感谢MapStruct聊天,我把它添加到 EditorMapper
这里是我的情况下,非常常见的我想:我有2个DTO的引用对方:
public class BookDTO {
private Long id;
private String title;
//... other properties
//@JsonManagedReference --> not necessary anymore
private EditorDTO editor;
}
public class EditorDTO {
private Long id;
private String name;
//...other properties
//@JsonBackReference --> not necessary anymore
private List< BookDTO > bookList;
}
我需要MapStruct能够在编辑器中的书目排除属性编辑器,然后避免无限循环。这是我目前拥有的映射器:
@Mapper
public interface BookMapper {
BookMapper INSTANCE = Mappers.getMapper( BookMapper.class );
@Mapping( target = "editor.bookList", ignore = true)
BookDTO toDTO( BookEntity bookEntity );
@Named( "NoEditor" )
@Mapping(target = "editor", ignore = true)
BookDTO toDTONoEditor( BookEntity bookEntity );
List<BookDTO> toDTOList( List<BookEntity> bookEntityList );
@Named( "NoEditor" )
@IterableMapping(qualifiedByName="NoEditor")
List<BookDTO> toDTOListNoEditor( List<BookEntity> bookEntityList );
@Mapping( target = "editor.bookList", ignore = true)
BookEntity toEntity( BookDTO bookDTO );
List<BookEntity> toEntityList( List<BookDTO> bookDTOList );
}
@Mapper(uses = BookMapper.class)
public interface EditorMapper {
EditorMapper INSTANCE = Mappers.getMapper( EditorMapper.class );
@Named( "NoEditor" )
@Mapping(target = "bookList", qualifiedByName = "NoEditor")
EditorDTO toDTO( EditorEntity editorEntity );
@Named( "NoEditor" )
@IterableMapping(qualifiedByName="NoEditor")
List<EditorDTO> toDTOList( List< EditorEntity > editorEntityList );
EditorEntity toEntity( EditorDTO editorDTO );
List<EditorEntity> toEntityList( List< EditorDTO > editorDTOList );
}
[编辑]:现在的作品,但它不是100%干净(请参阅下面我发布更多细节的答案)
我也试过这种方法在映射器,但它并没有对我的PB任何影响。
BookDTO toDTO( BookEntity bookEntity, @Context CycleAvoidingMappingContext context );
有谁知道我做错了吗?非常感谢!:)
[编辑]:我也添加了解决方案双向多对多映射感谢https://gitter.im/mapstruct/mapstruct-users#,我已经能够得到解决。我必须: -添加uses
attribut到EditorMapper
:@Mapper(componentModel = "spring", uses = BookMapper.class)
-添加的替代方法,如toDTONoEditor
或toDTOListNoEditor
在BookMapper
那里我忽略了editor
财产。-图论文在替代方法EditorMapper
-同为每个循环依赖
这里是解决方案:
BookDTO
public class BookDTO {
private Long id;
private String title;
//... other properties
private EditorDTO editor;
private List< CategoryDTO > categoryList;
}
EditorDTO
public class EditorDTO {
private Long id;
private String name;
//...other properties
private List< BookDTO > bookList;
}
CategoryDTO
public class CategoryDTO {
private Long id;
private String category;
private List< BookDTO > bookList;
}
BookMapper
@Mapper(componentModel = "spring", uses = CategoryMapper.class)
public interface BookMapper {
@Named( "NoBook" )
@Mappings( {
@Mapping(target = "categoryList", qualifiedByName = "NoBook"),
@Mapping( target = "editor.bookList", ignore = true)
} )
BookDTO toDTO( BookEntity bookEntity );
@Named( "NoEditor" )
@Mapping(target = "editor", ignore = true)
BookDTO toDTONoEditor( BookEntity bookEntity );
@Named( "NoCategory" )
@Mapping(target = "categoryList", ignore = true)
BookDTO toDTONoCategory( BookEntity bookEntity );
@Named( "NoBook" )
@IterableMapping(qualifiedByName="NoBook")
List<BookDTO> toDTOList( List<BookEntity> bookEntityList );
@Named( "NoCategory" )
@IterableMapping(qualifiedByName="NoCategory")
List<BookDTO> toDTOListNoCategory( List<BookEntity> bookEntityList );
@Named( "NoEditor" )
@IterableMapping(qualifiedByName="NoEditor")
List<BookDTO> toDTOListNoEditor( List<BookEntity> bookEntityList );
@Named( "NoBook" )
@Mappings( {
@Mapping(target = "categoryList", qualifiedByName = "NoBook"),
@Mapping( target = "editor.bookList", ignore = true)
} )
BookEntity toEntity( BookDTO bookDTO );
@Named( "NoCategory" )
@Mapping(target = "categoryList", ignore = true)
BookEntity toEntityNoCategory( BookDTO bookDTO );
@Named( "NoBook" )
@IterableMapping(qualifiedByName="NoBook")
List<BookEntity> toEntityList( List<BookDTO> bookDTOList );
@Named( "NoCategory" )
@IterableMapping(qualifiedByName="NoCategory")
List<BookEntity> toEntityListNoCategory( List<BookDTO> bookDTOList );
}
EditorMapper
@Mapper(componentModel = "spring", uses = BookMapper.class)
public interface EditorMapper {
@Named( "NoEditor" )
@Mapping(target = "bookList", qualifiedByName = "NoEditor")
EditorDTO toDTO( EditorEntity editorEntity );
@Named( "NoCategory" )
@Mapping(target = "bookList", ignore = true)
EditorDTO toDTONoBook( EditorEntity editorEntity );
@Named( "NoEditor" )
@IterableMapping(qualifiedByName="NoEditor")
List<EditorDTO> toDTOList( List< EditorEntity > editorEntityList );
@Named( "NoCategory" )
@IterableMapping(qualifiedByName="NoCategory")
List<EditorDTO> toDTOListNoCategory( List< EditorEntity > editorEntityList );
EditorEntity toEntity( EditorDTO editorDTO );
List<EditorEntity> toEntityList( List< EditorDTO > editorDTOList );
}
CategoryMapper
@Mapper(componentModel = "spring",uses = BookMapper.class)
public interface CategoryMapper {
@Named( "NoCategory" )
@Mapping(target = "bookList", qualifiedByName = "NoCategory")
CategoryDTO toDTO( CategoryEntity categoryEntity );
@Named( "NoBook" )
@Mapping(target = "bookList", ignore = true)
CategoryDTO toDTONoBook( CategoryEntity categoryEntity );
@Named( "NoCategory" )
@IterableMapping(qualifiedByName="NoCategory")
List<CategoryDTO> toDTOList( List< CategoryEntity > categoryEntityList );
@Named( "NoBook" )
@IterableMapping(qualifiedByName="NoBook")
List<CategoryDTO> toDTOListNoBook( List< CategoryEntity > categoryEntityList );
@Named( "NoCategory" )
@Mapping(target = "bookList", qualifiedByName = "NoCategory")
CategoryEntity toEntity( CategoryDTO categoryDTO );
@Named( "NoBook" )
@Mapping(target = "bookList", ignore = true)
CategoryEntity toEntityNoBook( CategoryDTO categoryDTO );
List<CategoryEntity> toEntityList( List< CategoryDTO > categoryDTOList );
@Named( "NoBook" )
@IterableMapping(qualifiedByName="NoBook")
List<CategoryEntity> toEntityListNoBook( List< CategoryDTO > categoryDTOList );
}
通过这种方式,它涉及到无限循环之前的循环依赖被破坏。然而,它是满足99%,因为Editor
和Book
对象不是完全干净。Editor
包含书目,很好。但是每本书中bookList
还包含一个空editor
场。而反之为Book
对象。但它似乎是一个德/序列化的问题,而不是一个MapStruct一个。以下是将JSON由此而来
编辑
{
"id": 1,
"name": "Folio",
"coordinates": null,
"bookList": [
{
"id": 1,
"title": "Le cycle de Fondation, I : Fondation",
"categoryList": [
{
"id": 5,
"category": "LITERATURE&FICTION"
}
],
"language": "French",
"isbn": 2070360539,
"publicationDate": null,
"numberOfPages": 416,
"authorList": [],
"libraryList": [
{
"id": 2,
"name": "Library2",
"coordinates": null
},
{
"id": 1,
"name": "Library1",
"coordinates": null
}
],
"editor": null
}
]
}
书
{
"id": 1,
"title": "Le cycle de Fondation, I : Fondation",
"categoryList": [
{
"id": 5,
"category": "LITERATURE&FICTION",
"bookList": null
}
],
"language": "French",
"isbn": 2070360539,
"publicationDate": null,
"numberOfPages": 416,
"authorList": [],
"libraryList": [
{
"id": 2,
"name": "Library2",
"coordinates": null
},
{
"id": 1,
"name": "Library1",
"coordinates": null
}
],
"editor": {
"id": 1,
"name": "Folio",
"coordinates": null,
"bookList": null
}
}
类别
{
"id": 1,
"category": "CHILDREN",
"bookList": [
{
"id": 5,
"title": "Le petit prince",
"categoryList": null,
"language": "French",
"isbn": 9782070612758,
"publicationDate": null,
"numberOfPages": 120,
"authorList": [],
"libraryList": [
{
"id": 2,
"name": "Library2",
"coordinates": null
},
{
"id": 1,
"name": "Library1",
"coordinates": null
}
],
"editor": null
}
]
}
希望这有助于:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句