【问题标题】:Getting nulls while using ModelMapper使用 ModelMapper 时获取空值
【发布时间】:2020-08-08 14:24:27
【问题描述】:

我正在尝试在转换过程中使用ModelMapper。我需要做的是将Sample 实体转换为SampleDTO 对象。

我有Sample 实体,如下所示:

@Entity
@Table(name = "sample", schema = "sample_schema")
@Data
@NoArgsConstructor
public class Sample {

    private static final String SEQUENCE = "SAMPLE_SEQUENCE";

    @Id
    @SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
    private Long id;

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

    @Column
    private String surname;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_deetails")
    private Details details;
}

其中持有Details一:

@Entity
@Table(name = "details", schema = "sample_schema")
@Data
@NoArgsConstructor
public class Details {

    private static final String SEQUENCE = "DETAILS_SEQUENCE";

    @Id
    @SequenceGenerator(sequenceName = SEQUENCE, name = SEQUENCE, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
    private Long id;

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

    @Column
    private String city;
}

我希望 DTO 采用这种格式:

@NoArgsConstructor
@AllArgsConstructor
@Data
public class SampleDTO {
    private Long id;
    private String name;
    private String surname;
    private String streetName;
    private String city;
}

我还制作了一个 ModelMapper bean,例如:

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

我做了一个转换器组件:

@Component
public class EntityDtoConverter {

    private final ModelMapper modelMapper;

    @Autowired
    public EntityDtoConverter(ModelMapper modelMapper) {
        this.modelMapper = modelMapper;
    }

    public SampleDTO sampleToDto(Sample entity) {
        return modelMapper.map(entity, SampleDTO.class);
    }
}

问题是

当我尝试在我的服务中使用此映射器转换器时

@Service
public class SampleService {

    private final SampleRepository sampleRepository;
    private final EntityDtoConverter entityDtoConverter;
    
    @Autowired
    public SampleService(SampleRepository sampleRepository, EntityDtoConverter entityDtoConverter) {
        this.sampleRepository = sampleRepository;
        this.entityDtoConverter = entityDtoConverter;
    }

    public List<SampleDTO> getSamples() {
        List<SampleDTO> samples = sampleRepository.findAll()
                .map(entityDtoConverter::sampleToDto);
        return new List<SampleDTO>(samplesPage);
    }
}

我在 Details 字段的地方得到空值。

我已经关注Baeldung's tutorial 关于使用 ModelMapper 的模型到 dto 转换以及它的 documentation,但至少没有太大帮助。我缺少一些东西,我不知道它是什么。

我正在努力:

  • Java 11
  • Spring Boot 2.3.0
  • 模型映射器 2.3.8

【问题讨论】:

    标签: java spring hibernate modelmapper


    【解决方案1】:

    试试:

    modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
    

    同时检查:Modelmapper: How to apply custom mapping when source object is null?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      相关资源
      最近更新 更多