[英]Why does this MapStruct generated class does not include import statement?
I am using MapStruct to map between JPA entities and POJO DTOs.我正在使用 MapStruct 在 JPA 实体和 POJO DTO 之间进行映射。
All my entities extend a common base class that has an ID field (a java.lang.Long
).我所有的实体都扩展了一个具有 ID 字段(
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.我有以下抽象映射器,它允许我从 JPA 中的关系映射到 DTO 中的简单长字段(或列表)。
An entity or List<entity>
field can be mapped to a Long
/ List<Long>
field, eg User.groups
could be mapped to UserDTO.groupIds
:实体或
List<entity>
字段可以映射到Long
/ List<Long>
字段,例如User.groups
可以映射到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 :然而,生成的实现类不包含
AbstractBaseEntity
类的任何导入语句,尽管它存在于抽象类中,因此代码不会编译:
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.我在 Java 8 中使用 MapStruct 1.0.0.Final。
EDIT: If I add an abstract method that uses the AbstractBaseEntity
class directly, then the import is added :编辑:如果我添加一个直接使用
AbstractBaseEntity
类的抽象方法,则添加导入:
public abstract AbstractBaseEntityDTO entityToDTO(AbstractBaseEntity abstractBaseEntity);
EDIT2: posted the issue on MapStruct's Github : https://github.com/mapstruct/mapstruct/issues/844 EDIT2:在 MapStruct 的 Github 上发布了这个问题: https : //github.com/mapstruct/mapstruct/issues/844
When missing an import with MapStruct, note that you can manually configure the Mapper to import it :当使用 MapStruct 丢失导入时,请注意您可以手动配置 Mapper 以导入它:
@Mapper( imports = AbstractBaseEntity.class )
Search for "Declaring an import" in MapStruct documentation在MapStruct 文档中搜索“声明导入”
(exemple 72 at the time of this edit : Mapstruct 1.4.1.Final) (本次编辑时的示例 72:Mapstruct 1.4.1.Final)
This was a bug in MapStruct 1.0.0.这是 MapStruct 1.0.0 中的一个错误。 It is (will be) fixed in the (at the time of writing, upcoming) version 1.1.0.
它(将)在(撰写本文时,即将发布)版本 1.1.0 中修复。
See : https://github.com/mapstruct/mapstruct/issues/844见: https : //github.com/mapstruct/mapstruct/issues/844
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.