英文:
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 = "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;
}
Which holds the Details
one:
@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;
}
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<SampleDTO> getSamples() {
List<SampleDTO> samples = sampleRepository.findAll()
.map(entityDtoConverter::sampleToDto);
return new List<SampleDTO>(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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论