Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

java - MapStruct casts error incompatible types: cannot be converted to

Not sure what happens here but when I do mvn clean install I get following error:

incompatible types: capture#1 of ? cannot be converted to com.sweetchoice.report.entities.WhoDelivers

Mapper class:

@Mapper(uses = {BaseJournalMapper.class})
public interface WhoDeliversMapper {

    WhoDelivers dtoToEntity(WhoDeliversDto whoDeliversDto);

    WhoDeliversDto entityToDto(WhoDelivers whoDelivers);
}

Entity class:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "who_delivers")
public class WhoDelivers extends BaseJournalEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Long id;

    private String name;
}

BaseJournalEntity/BaseJournalDto:

@Getter
@Setter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public class BaseJournalDto {

    private LocalDateTime createdAt;
    private Long createdBy;
    private LocalDateTime updatedAt;
    private Long updatedBy;
}

DTO class:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class WhoDeliversDto extends BaseJournalDto{

    private Long id;

    private String name;

}

and finally pom.xml:

     <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId> lombok-mapstruct-binding</artifactId>
                            <version>0.1.0</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>
enter code here

I'm using Java 11, MapStruct 1.4.1.Final and Lombok 1.18.16

question from:https://stackoverflow.com/questions/65862668/mapstruct-casts-error-incompatible-types-cannot-be-converted-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer :

Found an answer on my own.

Just add @SuperBuilder in Entity and DTO classes. More info on this link Documentation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...