Mapstruct:在自定义映射器方法中访问“ this”

时间:2019-01-09 21:06:57

标签: java mapstruct

给出以下将FarmInputDto映射到Farm的方法:

@Mapping(target = "assignment", source = "user", qualifiedByName = "addUserToAssignments")
public abstract Farm toFarm(FarmInputDto input, User user);

是否可以在自定义映射器方法中访问刚创建的Farm实例?

protected UserFarmAssignment addUserToAssignments(User user) {
    return new UserFarmAssignment(user, "$FARM_INSTANCE?");
}

我已经读过@MappingTarget,但这是行不通的(也许无论如何在这种情况下都不能这样使用):

protected List<UserFarmAssignment> addUserToAssignments(User user, @MappingTarget Farm farm) { }

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找@ObjectFactory。有关更多信息,请参见here