Why does this MapStruct generated class does not include import statement?
Asked Answered
V

3

10

I am using MapStruct to map between JPA entities and POJO DTOs.

All my entities extend a common base class that has an ID field (a java.lang.Long).

I have the following abstract mapper, that allows me to map from relationship in JPA to a simple Long field (or List) in the DTOs.

An entity or List<entity> field can be mapped to a Long/List<Long> field, e.g. User.groups could be mapped to UserDTO.groupIds :

@Mapper
public abstract class EntityMapper {

    public Long entityToLongId(AbstractBaseEntity entity){
        return entity.getId();
    }

    public abstract List<Long> entityCollectionToLongIdList(Collection<? extends AbstractBaseEntity> entities);
}

However the generated implementation class does not include any import statement for the AbstractBaseEntity class, although it is present in the abstract class, so the code does not compile :

package ....;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2016-07-27T12:11:25+0200",
    comments = "version: 1.0.0.Final, compiler: javac, environment: Java 1.8.0_66 (Oracle Corporation)"
)
@Component
public class EntityMapperImpl extends EntityMapper {

    @Override
    public List<Long> entityCollectionToLongIdList(Collection<? extends ch.unine.tango.model.AbstractBaseEntity> entities) {
        if ( entities == null ) {
            return null;
        }

        List<Long> list = new ArrayList<Long>();
        for ( AbstractBaseEntity abstractBaseEntity : entities ) { // compilation error here !
            list.add( entityToLongId( abstractBaseEntity ) );
        }

        return list;
    }
}

Why is that ? Am I doing it wrong ? How to fix this ?

I am using MapStruct 1.0.0.Final with Java 8.

EDIT: If I add an abstract method that uses the AbstractBaseEntity class directly, then the import is added :

public abstract AbstractBaseEntityDTO entityToDTO(AbstractBaseEntity abstractBaseEntity); 

EDIT2: posted the issue on MapStruct's Github : https://github.com/mapstruct/mapstruct/issues/844

Vortumnus answered 27/7, 2016 at 10:24 Comment(0)
V
0

This was a bug in MapStruct 1.0.0. It is (will be) fixed in the (at the time of writing, upcoming) version 1.1.0.

See : https://github.com/mapstruct/mapstruct/issues/844

Vortumnus answered 29/7, 2016 at 9:38 Comment(0)
M
43

When missing an import with MapStruct, note that you can manually configure the Mapper to import it :

@Mapper( imports = AbstractBaseEntity.class )

Search for "Declaring an import" in MapStruct documentation

(exemple 72 at the time of this edit : Mapstruct 1.4.1.Final)

Monosepalous answered 25/5, 2018 at 9:32 Comment(1)
Documentation update: see Expressions Example 72Birkle
K
3

As yunandtidus correctly states, you can solve it like this:

@Mapper( imports = AbstractBaseEntity.class )

To add to his response, if you need to import multiple classes, you just wrap them in curly brackets:

@Mapper( imports = {AbstractBaseEntity.class, MyOtherCoolClass.class} )
Kampmann answered 10/8, 2023 at 23:40 Comment(0)
V
0

This was a bug in MapStruct 1.0.0. It is (will be) fixed in the (at the time of writing, upcoming) version 1.1.0.

See : https://github.com/mapstruct/mapstruct/issues/844

Vortumnus answered 29/7, 2016 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.