Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在mapstruct Mapper中实现自定义行为的问题_Java_Spring_Spring Boot_Spring Data Jpa_Mapstruct - Fatal编程技术网

Java 在mapstruct Mapper中实现自定义行为的问题

Java 在mapstruct Mapper中实现自定义行为的问题,java,spring,spring-boot,spring-data-jpa,mapstruct,Java,Spring,Spring Boot,Spring Data Jpa,Mapstruct,我有以下DTO课程: @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class Conclusion { private Integer id; // ...... private List<CType> cTypes; } @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class CType {

我有以下DTO课程:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Conclusion {

    private Integer id;
    // ......
    private List<CType> cTypes;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CType {

    private Integer id;
    // ......
    private VType vType;
}
@Getter
@塞特
@诺尔格构装师
@AllArgsConstructor
公开课结论{
私有整数id;
// ......
私有列表类型;
}
@吸气剂
@塞特
@诺尔格构装师
@AllArgsConstructor
公共类CType{
私有整数id;
// ......
私有VType VType;
}
以及相应的实体类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "conclusion")
public class Conclusion {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    // ......

    @OneToMany
    @JoinColumn(name = "id")
    private List<CTypeEntity> cTypeEntities;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "c_types")
public class CTypeEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    // ......

    @Column(name = "v_type_id")
    private Integer vTypeId;
}
@Getter
@塞特
@诺尔格构装师
@AllArgsConstructor
@实体
@表(name=“结论”)
公开课结论{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”,nullable=false,updateable=false)
私有整数id;
// ......
@独身癖
@JoinColumn(name=“id”)
私人名单实体;
}
@吸气剂
@塞特
@诺尔格构装师
@AllArgsConstructor
@实体
@表(name=“c_类型”)
公共类CTypeEntity{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”,nullable=false,updateable=false)
私有整数id;
// ......
@列(name=“v\u type\u id”)
私有整数vTypeId;
}
此外,还存在所有相应的Dao和JPA存储库接口。 现在,我正在编写我的mapstruct Mapper接口,它应该用于将实体映射到DTO,反之亦然。这是地图绘制者:

@Mapper
public interface ConclusionMapper {

    @Mappings({
            @Mapping(target = "cTypes", source = "cTypeEntities")
    })
    Conclusion toConclusion(ConclusionEntity entity);

    List<Conclusion> toConclusionList(List<ConclusionEntity> entities);

    @Mappings({
            @Mapping(target = "cTypeEntities", source = "cTypes")
    })
    ConclusionEntity fromConclusion(Conclusion conclusion);

    List<ConclusionEntity> fromConclusionList(List<Conclusion> conclusions);

    @Mappings({
            @Mapping(target = "cType", source = "vTypeId", qualifiedByName = "formVType")
    })
    ConclusionTaxType toCType(CTypeEntity cTypeEntity);

    List<CType> toCTypes(List<CTypeEntity> cTypeEntities);

    CTypeEntity fromCType(CType cType);

    List<CTypeEntity> fromCTypeList(List<CType> cTypes);

    @Named("formVType")
    default VType formVType(CTypeEntity entity) {
        // TODO: instantiate a DAO which will return an instance of VType
        VTypeDao dao; // instantiate somehow

        return vTypeDao.findById(entity.getVId()); 
    }
}
@Mapper
公共接口结论映射器{
@映射({
@映射(target=“cTypes”,source=“cTypeEntities”)
})
结论与结论(结论实体);
列表到结论列表(列出实体);
@映射({
@映射(target=“cTypeEntities”,source=“cTypes”)
})
结论实体来源于结论(结论结论);
列表fromConclusionList(列表结论);
@映射({
@映射(target=“cType”、source=“vTypeId”、qualifiedByName=“formVType”)
})
结论分类类型toCType(CTypeEntity CTypeEntity);
列表类型(列表类型实体);
CTypeEntity fromCType(CType CType);
列表来自cTypeList(列表cTypes);
@命名(“formVType”)
默认VType formVType(CTypeEntity实体){
//TODO:实例化将返回VType实例的DAO
VTypeDao;//以某种方式实例化
返回vTypeDao.findById(entity.getVId());
}
}
VTypeDao看起来像这样:

public interface VTypeDao {
    VType findById(int id);

    boolean exists(int id);

    List<VType> findAll();
}

@Component
public class VTypeDaoImpl implements VTypeDao {
    private final VTypeRepo vTypeRepo;

    public VTypeDaoImpl(VTypeRepo vTypeRepo) {
        this.vTypeRepo = vTypeRepo;
    }

    // ............................. method implementations
}
公共接口VTypeDao{
VType findById(int-id);
布尔存在(int-id);
列出findAll();
}
@组成部分
公共类VTypeDaoImpl实现VTypeDao{
私人最终VTypeRepo VTypeRepo;
公共VTypeDaoImpl(VTypeRepo VTypeRepo){
this.vTypeRepo=vTypeRepo;
}
//方法实现
}
我的问题是:如何实例化VTypeDao的对象(或者至少是VTypeRepo,以便我可以将if作为参数传递给VTypeDaoImpl)

没有用于获取VTypeDao的适当实现的工厂类


编辑:VTypeDao及其实现是我的项目的第三方组件。

将map struct与spring集成:

@Mapper替换为以下内容

@Mapper(componentModel = "spring")    
并在
VTypeDaoImpl

@Autowired  
private ConclusionMapper conclusionMapper;
在maven命令下执行(假设您使用的是maven)

mvn干净编译


源代码将在
目标/生成的源代码/注释中生成
将map struct与spring集成:

@Mapper替换为以下内容

@Mapper(componentModel = "spring")    
并在
VTypeDaoImpl

@Autowired  
private ConclusionMapper conclusionMapper;
在maven命令下执行(假设您使用的是maven)

mvn干净编译


源代码将在
目标/生成的源代码/注释中生成

根据您的评论,我知道您希望在映射过程中查找VTypeDao。您可以将其包装在另一个类中,并将其作为@Context注释为映射参数传递。MaPrStUT不会考虑这样的类作为源或目标。但是,它将调用此类中的生命周期方法。。请看以下示例:


它是基于jpa的,但是您可以很容易地将其映射到您的问题。

从您的评论中,我发现您希望在映射过程中查找VTypeDao。您可以将其包装在另一个类中,并将其作为@Context注释为映射参数传递。MaPrStUT不会考虑这样的类作为源或目标。但是,它将调用此类中的生命周期方法。。请看以下示例:


它基于jpa,但您可以轻松地将其映射到您的问题。

我最终实现了以下功能:

@Mapper
public interface ConclusionMapper {

    @Mappings({
           @Mapping(target = "cTypes", source = "cTypeEntities")
    })
    Conclusion toConclusion(ConclusionEntity entity);

    List<Conclusion> toConclusionList(List<ConclusionEntity> entities);

    @InheritInverseConfiguration
    ConclusionEntity fromConclusion(Conclusion conclusion);

    List<ConclusionEntity> fromConclusionList(List<Conclusion> conclusions);

    @Mappings({
            @Mapping(target = "vType", ignore = true)
    })
    ConclusionTaxType toCType(CTypeEntity cTypeEntity);

    List<CType> toCTypes(List<CTypeEntity> cTypeEntities);

    @Mappings({
            @Mapping(target = "vTypeId", source = "vType.id")
    })
    CTypeEntity fromCType(CType cType);

    List<CTypeEntity> fromCTypeList(List<CType> cTypes);
}
@Mapper
公共接口结论映射器{
@映射({
@映射(target=“cTypes”,source=“cTypeEntities”)
})
结论与结论(结论实体);
列表到结论列表(列出实体);
@继承反向配置
结论实体来源于结论(结论结论);
列表fromConclusionList(列表结论);
@映射({
@映射(target=“vType”,ignore=true)
})
结论分类类型toCType(CTypeEntity CTypeEntity);
列表类型(列表类型实体);
@映射({
@映射(target=“vTypeId”,source=“vType.id”)
})
CTypeEntity fromCType(CType CType);
列表来自cTypeList(列表cTypes);
}

因此,我只是忽略了实体到DTO映射中的vType成员,并将其手动放入我的DAO中,因为这是最简单的方法。

我最终得到了以下实现:

@Mapper
public interface ConclusionMapper {

    @Mappings({
           @Mapping(target = "cTypes", source = "cTypeEntities")
    })
    Conclusion toConclusion(ConclusionEntity entity);

    List<Conclusion> toConclusionList(List<ConclusionEntity> entities);

    @InheritInverseConfiguration
    ConclusionEntity fromConclusion(Conclusion conclusion);

    List<ConclusionEntity> fromConclusionList(List<Conclusion> conclusions);

    @Mappings({
            @Mapping(target = "vType", ignore = true)
    })
    ConclusionTaxType toCType(CTypeEntity cTypeEntity);

    List<CType> toCTypes(List<CTypeEntity> cTypeEntities);

    @Mappings({
            @Mapping(target = "vTypeId", source = "vType.id")
    })
    CTypeEntity fromCType(CType cType);

    List<CTypeEntity> fromCTypeList(List<CType> cTypes);
}
@Mapper
公共接口结论映射器{
@映射({
@映射(target=“cTypes”,source=“cTypeEntities”)
})
结论与结论(结论实体);
列表到结论列表(列出实体);
@继承反向配置
结论实体来源于结论(结论结论);
列表fromConclusionList(列表结论);
@映射({
@Mapp