Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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 带有Spring数据Jdbc的Mapstruct_Java_Spring_Spring Boot_Spring Data Jpa_Mapstruct - Fatal编程技术网

Java 带有Spring数据Jdbc的Mapstruct

Java 带有Spring数据Jdbc的Mapstruct,java,spring,spring-boot,spring-data-jpa,mapstruct,Java,Spring,Spring Boot,Spring Data Jpa,Mapstruct,我使用的是Spring数据Jdbc,我有两个与引用id相关的聚合 public class ResourceEntity { @Id @With private final UUID id; private String institutionId; private String version; private Long resourceTypeId; public class ResourceTypeEntity { @Id @With private final Long id;

我使用的是Spring数据Jdbc,我有两个与引用id相关的聚合

public class ResourceEntity {

@Id
@With
private final UUID id;
private String institutionId;
private String version;  
private Long resourceTypeId;

public class ResourceTypeEntity {

@Id @With
private final Long id;
private String name;
我想将其映射为将被翻译的GRPC消息

public class Resource {
    private String institutionId;
    private String version;  
    private String name; <-- This should be mapped after lookup the ResourceTypeEntity byId
}
公共类资源{
私人字符串机构ID;
私有字符串版本;

私有字符串名称;您没有机会通过接口实现这一点,只要您需要自动连接存储库以获取对象及其名称以进行进一步映射。但是,您可以使用
抽象类
,该类与生成MapStruct类(包括Spring组件模型)完全兼容。

@Mapper(
        componentModel = "spring",
        unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE,
        collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public abstract static class ResourceMapper {

    @Autowired
    private ResourceTypeRepository repository;

    public abstract Resource toResource(ResourceEntity resourceEntity);

    public abstract List<Resource> toResources(List<ResourceEntity> resourceEntities);

    @AfterMapping
    public void afterMapping(@MappingTarget Resource resource, ResourceEntity entity) {
        
        long id = resourceEntity.getResourceTypeId();

        // Call the repository to fetch ResourceTypeEntity by resourceTypeId
        // The method results in Optional<ResourceTypeEntity> so you might want to 
        // ... throw an exception or use a default value if no entity by id is found
        String name = repository.findById(id)
            .map(ResourceTypeEntity::getName)
            .orElse(null);                      

        resource.setName(name);
    }
}
请记住,这是一个服务而不仅仅是一个实体dto映射器,只要其中的逻辑不是琐碎的,并且依赖于数据库连接和数据。我宁愿创建一个
@service
,它使用
接口资源映射器
和一个不获取数据的方法(改为服务层)但通过
@Context
传递
name

Resource toResource(ResourceEntity entity, @Context String name);

@AfterMapping
void toResourceAfterMapping(
    @MappingTarget Resource resource, ResourceEntity entity, @Context String name
) {
    resource.setName(name);
}

只要您需要自动连接存储库以获取对象及其名称以进行进一步映射,就没有机会通过接口实现这一点。但是,您可以使用
抽象类
,它与MapStruct类(包括Spring组件模型)完全兼容

@Mapper(
        componentModel = "spring",
        unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE,
        collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public abstract static class ResourceMapper {

    @Autowired
    private ResourceTypeRepository repository;

    public abstract Resource toResource(ResourceEntity resourceEntity);

    public abstract List<Resource> toResources(List<ResourceEntity> resourceEntities);

    @AfterMapping
    public void afterMapping(@MappingTarget Resource resource, ResourceEntity entity) {
        
        long id = resourceEntity.getResourceTypeId();

        // Call the repository to fetch ResourceTypeEntity by resourceTypeId
        // The method results in Optional<ResourceTypeEntity> so you might want to 
        // ... throw an exception or use a default value if no entity by id is found
        String name = repository.findById(id)
            .map(ResourceTypeEntity::getName)
            .orElse(null);                      

        resource.setName(name);
    }
}
请记住,这是一个服务而不仅仅是一个实体dto映射器,只要其中的逻辑不是琐碎的,并且依赖于数据库连接和数据。我宁愿创建一个
@service
,它使用
接口资源映射器
和一个不获取数据的方法(改为服务层)但通过
@Context
传递
name

Resource toResource(ResourceEntity entity, @Context String name);

@AfterMapping
void toResourceAfterMapping(
    @MappingTarget Resource resource, ResourceEntity entity, @Context String name
) {
    resource.setName(name);
}

我最终使用了一个包含已返回名称的联接的查询。我想我确实尝试了类似的代码,但autowired存储库为null。并且@context没有在resourceMapperImpl中创建after方法。我肯定遗漏了一些微妙的东西。我最终使用了一个包含已返回名称的联接的查询。我想我确实尝试了类似的代码t自动连线存储库为空。@context没有在resourceMapperImpl中创建after方法。。我肯定缺少一些微妙的东西