使用ModelMapper时出现空值。

huangapple go评论54阅读模式
英文:

Getting nulls while using ModelMapper

问题

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

我有以下样本实体:

@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字段的位置得到了null。

我遵循了Baeldung的关于使用ModelMapper进行模型到DTO转换的教程以及它的文档,但后者没有太多帮助。有些东西我遗漏了,但我不知道是什么。

我正在使用:

  • Java 11
  • Spring Boot 2.3.0
  • ModelMapper 2.3.8
英文:

I'm trying to utilize the ModelMapper in my convertion process. What I need to do is to convert the Sample entity to SampleDTO object.

I have the Sample entity like the following:

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

    private static final String SEQUENCE = &quot;SAMPLE_SEQUENCE&quot;;

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

    @Column(name = &quot;name&quot;)
    private String name;

    @Column
    private String surname;

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

Which holds the Details one:

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

    private static final String SEQUENCE = &quot;DETAILS_SEQUENCE&quot;;

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

    @Column(name = &quot;street_name&quot;)
    private String streetName;

    @Column
    private String city;
}

I'd like the DTO to be this format:

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

I also made a ModelMapper bean like:

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

And I made a converter component:

@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);
    }
}

The problem is

when I try to use this mapper converter in my service

@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&lt;SampleDTO&gt; getSamples() {
        List&lt;SampleDTO&gt; samples = sampleRepository.findAll()
                .map(entityDtoConverter::sampleToDto);
        return new List&lt;SampleDTO&gt;(samplesPage);
    }
}

I get nulls in places of Details fields.

I have followed Baeldung's tutorial about model-to-dto conversion with ModelMapper and the documentation of it as well but the least wasn't much of help. There is something I'm missing and I have no idea what it is.

I'm working on:

  • Java 11
  • Spring Boot 2.3.0
  • ModelMapper 2.3.8

答案1

得分: 2

尝试:

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


还要检查:[Modelmapper:当源对象为null时如何应用自定义映射?][1]

  [1]: https://stackoverflow.com/questions/37971193/modelmapper-how-to-apply-custom-mapping-when-source-object-is-null

<details>
<summary>英文:</summary>

Try: 

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


Also check: [Modelmapper: How to apply custom mapping when source object is null?][1]


  [1]: https://stackoverflow.com/questions/37971193/modelmapper-how-to-apply-custom-mapping-when-source-object-is-null

</details>



huangapple
  • 本文由 发表于 2020年8月7日 17:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63299367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定