English 中文(简体)
mapstruct type conversion for different field types doesnt works
原标题:

In quarkus project I try to map DataSyncConfigurationMapping and DataSyncConfigurationMappingEntity with using MapStruct 1.5.5.Final version, except columnNames/columnNameList fields other field types matches and when tested the other field mappings works smoothly. What i have missed, any helps?

public class DataSyncConfigurationMappingEntity{
    private Long id;
    private String columnNames;
    private String oldValue;
    private String newValue;
    private DataSyncConfigurationEntity dataSyncConfiguration;
}
public class DataSyncConfigurationMapping{
    private Long id;
    private List<String> columnNameList;
    private String oldValue;
    private String newValue;
    private DataSyncConfiguration dataSyncConfiguration;
}
public class DataSyncConfigurationEntity{
    private Long id;
    private String name;
    private String fileName;
    private String sqlStatement;
    private String columnNames;
    private String cron;
}
public class DataSyncConfiguration{
    private Long id;
    private String name;
    private String fileName;
    private String sqlStatement;
    private List<String> columnNameList;
    private String cron;
}
@Mapper(componentModel = "jakarta", uses = {DataSyncConfigurationMapper.class})
public interface DataSyncConfigurationMappingMapper {
    @Mapping(source = "dataSyncConfiguration.columnNames", target = "dataSyncConfiguration.columnNameList",
            qualifiedByName = "stringToListConverter")
    DataSyncConfigurationMapping entityToPojo(DataSyncConfigurationMappingEntity entity);
    @Mapping(source = "dataSyncConfiguration.columnNameList", target = "dataSyncConfiguration.columnNames",
            qualifiedByName = "listToStringConverter")
    DataSyncConfigurationMappingEntity pojoToEntity(DataSyncConfigurationMapping pojo);
    @Named("stringToListConverter")
    default List<String> map(String value) {
        return stringToList(value);
    }
    @Named("listToStringConverter")
    default String map(List<String> list) {
        return listToString(list);
    }
}
@Mapper(componentModel = "jakarta")
public interface DataSyncConfigurationMapper {
    @Mapping(source = "columnNames", target = "columnNameList")
    DataSyncConfiguration entityToPojo(DataSyncConfigurationEntity entity);
    @Mapping(source = "columnNameList", target = "columnNames")
    DataSyncConfigurationEntity pojoToEntity(DataSyncConfiguration pojo);
    default List<String> map(String value){
        return stringToList(value);
    }
    default String map(List<String> list){
        return listToString(list);
    }
}
@ApplicationScoped
public class MapperUtils {
    public static List<String> stringToList(String columnNames) {
        if (columnNames == null) {
            return null;
        }        return Arrays.asList(columnNames.split(","));
    }
    public static String listToString(List<String> list) {
        if (list == null) {
            return null;
        }
        return String.join(",", list);
    }
}
@QuarkusTest
public class DataSyncConfigurationMappingMapperTest {
    @Inject
    DataSyncConfigurationMappingMapper dataSyncConfigurationMappingMapper;
        @Test
    public void testEntityToPojo() {
        DataSyncConfigurationMappingEntity entity = new DataSyncConfigurationMappingEntity();
        entity.setColumnNames("column1,column2,column3");
        entity.setOldValue("name");
        DataSyncConfigurationEntity dataSyncConfigurationEntity = new DataSyncConfigurationEntity();
        dataSyncConfigurationEntity.setName("sss");
        dataSyncConfigurationEntity.setColumnNames("a1,b2,c3");
        entity.setDataSyncConfiguration(dataSyncConfigurationEntity);
        DataSyncConfigurationMapping pojo = dataSyncConfigurationMappingMapper.entityToPojo(entity);
        assertEquals(entity.getId(), pojo.getId());
        assertEquals(Arrays.asList(entity.getColumnNames().split(",")), pojo.getColumnNameList());
        assertNotNull(pojo.getDataSyncConfiguration());
        assertEquals(dataSyncConfigurationEntity.getName(), pojo.getDataSyncConfiguration().getName());
        assertEquals(Arrays.asList(dataSyncConfigurationEntity.getColumnNames().split(",")), pojo.getDataSyncConfiguration().getColumnNameList());
    }
}

Like i mention on my code i tried to use stringToListConverter and listToStringConverter named methods to convert, but couldn t accomplished for this fields.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签