Closed
Description
My setup: Java 11.02, Gradle 5.2.1, MapStruct 1.3.0.Final, Lombok 1.18.6 and Spring Boot 2.3.1.
build.gradle
plugins {
id "net.ltgt.apt" version "0.21"
id "net.ltgt.apt-idea" version "0.21"
}
...
/* Lombok */
compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
/* Map Struct */
implementation "org.mapstruct:mapstruct-jdk8:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
FirstEntity.java
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
@Entity
@Table(name="first_entity")
public class FirstEntity {
@Id
private Long id;
@ManyToOne
@JoinColumn(name="second_entity_id")
private SecondEntity secondEntity;
}
FirstEntityDTO.java
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FirstEntityDTO {
private Long id;
private SecondEntityDTO secondEntity;
}
SecondEntity.java
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
@Entity
@Table(name="second_entity")
public class SecondEntity {
@Id
private Long id;
}
SecondEntityDTO.java
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SecondEntityDTO {
private Long id;
}
EntityMapper.java
@Mapper(
unmappedTargetPolicy = ReportingPolicy.IGNORE,
componentModel = "spring"
)
public interface EntityMapper {
FirstEntity updateEntity(FirstEntityDTO entityDto, @MappingTarget FirstEntity entity);
}
Generated code, MapStruct 1.3.0.Final
if (entityDto.getSecondEntity() != null ) {
if (entity.getSecondEntity() == null ) {
entity.setSecondEntity( SecondEntity.builder() ); //compilation error: incompatible types
}
secondEntityDTOToSecondEntity( entityDto.getSecondEntity(), entity.getSecondEntity());
}
Generated code, MapStruct 1.2.0.Final
if (entityDto.getSecondEntity() != null ) {
if (entity.getSecondEntity() == null ) {
entity.setSecondEntity( new SecondEntity() ); //success
}
secondEntityDTOToSecondEntity( entityDto.getSecondEntity(), entity.getSecondEntity());
}
As a workaround, I use MapStruct 1.2.0.Final now.