mapstruct@manytomy双向Map

xmakbtuz  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(246)

我有一个 @ManyToMany 关系,我正在使用mapstructMap持久层和域层。我的问题和这里解释的很相似。但我的情况和以前略有不同 @ManyToMany 与…相反 @OneToOne 问题中解释的关系。

@Table(name  = "user")
@Entity
public class UserEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "user_id",updatable = false)
    @Type(type = "uuid-char")
    private UUID userId;

    @ManyToMany
    @JoinTable(
            name = "user_product_mapping",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "product_id"))
    private List<ProductEntity> products;
}

这是我的另一个实体:

@Table(name = "product")
@Entity
public class ProductEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer Id;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "code",updatable = false)
    private String code;

    @ManyToMany(mappedBy = "products")
    private List<UserEntity> users;
}

两个类的Map都是:

@Mapper(componentModel = "spring", uses = {ProductMapper.class})
public interface UserMapper {

    @InheritInverseConfiguration
    UserDto toUserDto(UserEntity user, @Context MappingContext context);

    UserEntity userDtoToUser(UserDto userDto, @Context MappingContext context);

    List<UserDto> toUserDtos(List<UserEntity> users);
}

@Mapper(componentModel = "spring"
        ,uses = {UserMapper.class}
         )
public interface ProductMapper {

    @InheritInverseConfiguration
    ProductDto productToProductDto(ProductEntity product,@Context MappingContext context);

    ProductEntity productDtoToProduct(ProductDto productDto, MappingContext context);

    List<ProductDto> toProductDtos(List<ProductEntity> products);
}

有什么遗漏吗?我真的被困在这里了,因为我对mapstruct还很陌生。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题