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