alyssa moon
alyssa moon

Reputation: 35

Mapstruct problem: Unknown property error

Im using mapstruct and lombok together and experiencing some problems with it:

EntityMapper.java:10: error: Unknown property "id" in result type Entity. Did you mean "null"?
    @Mapping(target = "id", ignore = true)
                      ^

My Entity and EntityDto classes:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Entity {

    private int id;

    private String property;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class EntityDto {

    private String property;
}

EntityMapper:

@Mapper(implementationName = "MapStruct<CLASS_NAME>")
public interface EntityMapper {

    // neither of them work
    @Mapping(target = "id", ignore = true)
    //@Mapping(target = "id", defaultValue = "0")
    Entity map(EntityDto dto);

    EntityDto map(Entity entity);

}

In this configuration it leads to compile-time error. So i tried to comment out @Mapping annotation. It compiled, but it maps all properties to null. MapStructEntityMapper generated implementation:

public class MapStructEntityMapper implements EntityMapper {
    public MapStructEntityMapper() {
    }

    public Entity map(EntityDto dto) {
        if (dto == null) {
            return null;
        } else {
            Entity entity = new Entity();
            return entity;
        }
    }

    public EntityDto map(Entity entity) {
        if (entity == null) {
            return null;
        } else {
            EntityDto entityDto = new EntityDto();
            return entityDto;
        }
    }
}

I found couple answers that are talking about annotation processors, but take a look at my build.gradle file:

// MapStruct - Entity-DTO mapper                             
implementation 'org.mapstruct:mapstruct:1.4.1.Final'         
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.
compile 'org.projectlombok:lombok-mapstruct-binding:0.1.0'   
// Util                                                      
// lombok                                                    
compileOnly 'org.projectlombok:lombok:1.18.16'               
annotationProcessor 'org.projectlombok:lombok:1.18.16'       
testCompileOnly 'org.projectlombok:lombok:1.18.16'           
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'   

Some time it worked if i compile without @Mapping annotation and then run with this annotation, but now even this does not work.

Upvotes: 1

Views: 5788

Answers (2)

Joshua Enos
Joshua Enos

Reputation: 61

annotationProcessor for mapstruct should be below annotationProcessor for lombok.

Upvotes: 0

Filip
Filip

Reputation: 21423

This seems like a problem with the lombok-mapstruct-binding.

That should be in the same scope as the processors. You need to put it under annotationProcessor and not compile

Upvotes: 5

Related Questions