我是MapStruct的新手,所以我可能做的很愚蠢。提前致歉。我使用Spring Boot 1.5.3和JHipster使用JDL生成代码。 MapStruct用于将Entity映射到DTO,反之亦然。
我有一个对象Currency,它包含货币的主要信息,例如货币代码,货币符号等。CurrencyTl类以应用程序支持的各种语言存储货币的描述性信息,例如名称,描述等。语言包含应用程序支持的语言的定义。
货币
public class Currency extends AbstractEffectiveEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(name = "id", columnDefinition = "CHAR", length = 36, nullable = false)
private String id;
@Column(name = "currency_code", columnDefinition = "CHAR", length = 5, nullable = false, updatable = false)
private String currencyCode;
@Column(name = "currency_symbol", columnDefinition = "VARCHAR", length = 10, nullable = false, updatable = false)
private String currencySymbol;
@Column(name = "installed_flag", columnDefinition = "CHAR", length = 1, nullable = false)
private String installedFlag;
@OneToMany(mappedBy = "currency")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<CurrencyTl> currencyTls = new HashSet<>();
// Getters and Setters (Avoided for brevity)
}
CurrencyTl
public class CountryTl extends AbstractEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(columnDefinition = "CHAR", length = 36, nullable = false)
private String id;
@Column(name = "entity_name", columnDefinition = "VARCHAR", length = 120, nullable = false)
private String entityName;
@Column(name = "entity_desc", columnDefinition = "VARCHAR", length = 256, nullable = true)
private String entityDesc;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false)
private Country country;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "language_id", nullable = false)
private Language language;
// Getters and Setters
}
语言
public class Language extends AbstractEffectiveEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(name = "id", columnDefinition = "CHAR", length = 36, nullable = false)
private String id;
@Column(name = "language_code", columnDefinition = "CHAR", length = 5, nullable = false)
private String languageCode;
@Column(name = "language_name", columnDefinition = "VARCHAR", length = 80, nullable = false)
private String languageName;
// Getters and Setters
}
映射器界面
public interface EntityMapper<D, E> {
E toEntity(D dto);
D toDto(E entity);
List<E> toEntity(List<D> dtoList);
List<D> toDto(List<E> entityList);
}
我通过扩展上面的界面为Country,Language和CountryTl创建了Mapper界面。 CountryTlMapper接口定义如下:
CountryTlMapper
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN, uses = {LanguageMapper.class, CurrencyMapper.class})
public interface CurrencyTlMapper extends EntityMapper<CurrencyTlDTO, CurrencyTl> {
@Mapping(source = "language.id", target = "languageId")
@Mapping(source = "currency.id", target = "currencyId")
@Override
CurrencyTlDTO toDto(CurrencyTl currencyTl);
@Mapping(source = "languageId", target = "language")
@Mapping(source = "currencyId", target = "currency")
@Override
CurrencyTl toEntity(CurrencyTlDTO currencyTlDTO);
default CurrencyTl fromId(String id) {
if (id == null) {
return null;
}
CurrencyTl currencyTl = new CurrencyTl();
currencyTl.setId(id);
return currencyTl;
}
}
以上结果导致MapStruct生成实现类,如下所示:
CurrencyTlMapperImpl
@Component
public class CurrencyTlMapperImpl implements CurrencyTlMapper {
@Autowired
private LanguageMapper languageMapper;
@Autowired
private CurrencyMapper currencyMapper;
@Override
public List<CurrencyTl> toEntity(List<CurrencyTlDTO> dtoList) {
// Removed for brevity
return list;
}
@Override
public List<CurrencyTlDTO> toDto(List<CurrencyTl> entityList) {
// Removed for brevity
return list;
}
@Override
public CurrencyTlDTO toDto(CurrencyTl currencyTl) {
if ( currencyTl == null ) {
return null;
}
CurrencyTlDTO currencyTlDTO = new CurrencyTlDTO();
String id = currencyTlLanguageId( currencyTl );
if ( id != null ) {
currencyTlDTO.setLanguageId( Long.parseLong( id ) );
}
String id1 = currencyTlCurrencyId( currencyTl );
if ( id1 != null ) {
currencyTlDTO.setCurrencyId( Long.parseLong( id1 ) );
}
currencyTlDTO.setId( currencyTl.getId() );
currencyTlDTO.setEntityName( currencyTl.getEntityName() );
currencyTlDTO.setEntityDesc( currencyTl.getEntityDesc() );
return currencyTlDTO;
}
@Override
public CurrencyTl toEntity(CurrencyTlDTO currencyTlDTO) {
if ( currencyTlDTO == null ) {
return null;
}
CurrencyTl currencyTl = new CurrencyTl();
if ( currencyTlDTO.getLanguageId() != null ) {
currencyTl.setLanguage( languageMapper.fromId( String.valueOf( currencyTlDTO.getLanguageId() ) ) );
}
if ( currencyTlDTO.getCurrencyId() != null ) {
currencyTl.setCurrency( currencyMapper.fromId( String.valueOf( currencyTlDTO.getCurrencyId() ) ) );
}
currencyTl.setId( currencyTlDTO.getId() );
currencyTl.setEntityName( currencyTlDTO.getEntityName() );
currencyTl.setEntityDesc( currencyTlDTO.getEntityDesc() );
return currencyTl;
}
private String currencyTlLanguageId(CurrencyTl currencyTl) {
if ( currencyTl == null ) {
return null;
}
Language language = currencyTl.getLanguage();
if ( language == null ) {
return null;
}
String id = language.getId();
if ( id == null ) {
return null;
}
return id;
}
private String currencyTlCurrencyId(CurrencyTl currencyTl) {
if ( currencyTl == null ) {
return null;
}
Currency currency = currencyTl.getCurrency();
if ( currency == null ) {
return null;
}
String id = currency.getId();
if ( id == null ) {
return null;
}
return id;
}
}
CurrencyDTO
public class CurrencyDTO extends AbstractEffectiveEntityDTO implements Serializable {
private static final long serialVersionUID = -3989102119178748641L;
@Size(max = 36)
private String id;
@NotNull
@Size(min = 2, max = 5)
private String currencyCode;
@NotNull
@Size(min = 2, max = 10)
private String currencySymbol;
@NotNull
@Size(max = 1)
private String installedFlag;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public String getCurrencySymbol() {
return currencySymbol;
}
public void setCurrencySymbol(String currencySymbol) {
this.currencySymbol = currencySymbol;
}
public String getInstalledFlag() {
return installedFlag;
}
public void setInstalledFlag(String installedFlag) {
this.installedFlag = installedFlag;
}
}
CurrencyTlDTO
public class CurrencyTlDTO implements Serializable {
private static final long serialVersionUID = 1142708918251175249L;
@Size(max = 36)
private String id;
@NotNull
@Size(max = 120)
private String entityName;
@Size(max = 256)
private String entityDesc;
private Long languageId;
private Long currencyId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public String getEntityDesc() {
return entityDesc;
}
public void setEntityDesc(String entityDesc) {
this.entityDesc = entityDesc;
}
public Long getLanguageId() {
return languageId;
}
public void setLanguageId(Long languageId) {
this.languageId = languageId;
}
public Long getCurrencyId() {
return currencyId;
}
public void setCurrencyId(Long currencyId) {
this.currencyId = currencyId;
}
}
LanguageDTO
public class LanguageDTO extends AbstractEffectiveEntityDTO implements Serializable {
private static final long serialVersionUID = 8419729079888691835L;
private String id;
@NotNull
@Size(min = 2, max = 5)
private String languageCode;
@NotNull
@Size(min = 5, max = 80)
private String languageName;
@NotNull
@Size(min = 3, max = 80)
private String languageNativeName;
@Size(max = 256)
private String languageIcon;
@NotNull
@Size(max = 1)
private String installedFlag;
@NotNull
@Size(max = 1)
private String defaultFlag;
public LanguageDTO() {
// Emply constructor need for Jaskson
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLanguageCode() {
return languageCode;
}
public void setLanguageCode(String languageCode) {
this.languageCode = languageCode;
}
public String getLanguageName() {
return languageName;
}
public void setLanguageName(String languageName) {
this.languageName = languageName;
}
public String getLanguageNativeName() {
return languageNativeName;
}
public void setLanguageNativeName(String languageNativeName) {
this.languageNativeName = languageNativeName;
}
public String getLanguageIcon() {
return languageIcon;
}
public void setLanguageIcon(String languageIcon) {
this.languageIcon = languageIcon;
}
public String getInstalledFlag() {
return installedFlag;
}
public void setInstalledFlag(String installedFlag) {
this.installedFlag = installedFlag;
}
public String getDefaultFlag() {
return defaultFlag;
}
public void setDefaultFlag(String defaultFlag) {
this.defaultFlag = defaultFlag;
}
}
我不确定是什么原因使MapStruct将语言和货币的 id 视为 Long !该行失败currencyTlDTO.setCurrencyId( Long.parseLong( id1 ) )
,因为所有 id 属性都是字符串,并以1edd1914-783d-11e8-9e18-9801a7b6295b
的形式存储UUID值。有人可以建议我错在哪里,以及如何在MapStruct接受定义的属性的数据类型的情况下解决以上问题?预先感谢。
答案 0 :(得分:0)
不确定确切的问题是什么。但是,CurrencyTlDTO
的{{1}}类型的属性为Long
您通过以下方式明确地映射到其中:
currencyId
在@Mapping(source = "language.id", target = "languageId")
@Mapping(source = "currency.id", target = "currencyId")
中,您拥有
CurrencyTlDTO