MapStruct:如何使用mapper映射具有复合键JPA的视图对象?
我有如下目标实体:MapStruct:如何使用mapper映射具有复合键JPA的视图对象?,jpa,mapstruct,Jpa,Mapstruct,我有如下目标实体: public class ApplicationUsage { @Id private ApplicationUsagePK appUsage; //Guid No documentation available. private String name; . . public ApplicationUsage(String id, Integer compId) { this.appUsage = new
public class ApplicationUsage {
@Id
private ApplicationUsagePK appUsage; //Guid No documentation available.
private String name;
.
.
public ApplicationUsage(String id, Integer compId) {
this.appUsage = new ApplicationUsagePK(id, compId); //This is present in ApplicationUsagePK initiated here which acts are composite key
}
ApplicationUsage aUsage = ApplicationUsageMapper.MAPPER.entityToDao(agreement.getBody());
aUsage.initiateApplicationUsage(agreement.getBody().getId(), compId);
源对象如下所示:
public class ApplicationUsage {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
我需要将源对象的ID映射到ApplicationSage(字符串ID和CompID来自其他变量,它需要映射到ApplicationSage(..,整数CompID)
在上述类或映射器中需要进行哪些更改才能实现相同的效果
以下是当前的映射程序:
@Mapper
public interface ApplicationUsageMapper {
ApplicationUsageMapper MAPPER = Mappers.getMapper( ApplicationUsageMapper.class );
ApplicationUsage entityToDao(com.inte.meow.vo.computer.ApplicationUsage source);
}
坏方法:
我使用了一种不好的方法来解决这个问题,您可以理解以下内容:
public class ApplicationUsage {
@Id
private ApplicationUsagePK appUsage; //Guid No documentation available.
private String name;
.
.
public ApplicationUsage(String id, Integer compId) {
this.appUsage = new ApplicationUsagePK(id, compId); //This is present in ApplicationUsagePK initiated here which acts are composite key
}
ApplicationUsage aUsage = ApplicationUsageMapper.MAPPER.entityToDao(agreement.getBody());
aUsage.initiateApplicationUsage(agreement.getBody().getId(), compId);
您可以使用
@Context
注释来完成
@Mapper
public interface ApplicationUsageMapper {
ApplicationUsageMapper MAPPER = Mappers.getMapper( ApplicationUsageMapper.class );
@Mapping(target = "appUsage", source = "source", qualifiedByName = "getAppUsage")
ApplicationUsage entityToDao(com.inte.meow.vo.computer.ApplicationUsage source, @Context Integer compId);
@Named("getAppUsage")
default ApplicationUsagePK toAppUsage(com.inte.meow.vo.computer.ApplicationUsage source, @Context Integer compId) {
return new ApplicationUsagePK (source.getId(), compId));
}
}