JMapper - 映射双向关系的正确方法

时间:2017-02-11 16:21:21

标签: java

使用jMapper映射双向关系的正确方法是什么?

我试图使用@JMapConversion注释,但我无法阻止无限循环。

这是我的代码:

public class SourceClass1 {
    private SourceClass2 sourceClass2;

    //others attributes, getters and setters


}

public class SourceClass2 {
    private SourceClass1 sourceClass1;

    //others attributes, getters and setters

}

public class DestinationClass1 {
    private DestinationClass2 destinationClass2;

    //others attributes, getters and setters

    @JMapConversion(from="sourceClass2", to="destinationClass2", type=Type.DYNAMIC)
    public static String destinationClass2Conversion(){
        StringBuffer stBuffer = new StringBuffer();
        stBuffer.append("if (${destination} != null && ${destination}.getName() != null && !${destination}.getName().isEmpty()) {")
        .append("return ${destination};")
        .append('}')
        .append("return JMapConverter.toDestinationClass2(${destination}, ${source});");

        return stBuffer.toString();

    }
}

public class DestinationClass2 {
    private DestinationClass1 destinationClass1;

    //others attributes, getters and setters

    @JMapConversion(from="sourceClass1", to="destinationClass1", type=Type.DYNAMIC)
    public static String destinationClass1Conversion(){
        StringBuffer stBuffer = new StringBuffer();
        stBuffer.append("if (${destination} != null && ${destination}.getId() != null && !${destination}.getId().isEmpty()) {")
        .append("return ${destination};")
        .append('}')
        .append("return JMapConverter.toDestinationClass1(${destination}, ${source});");

        return stBuffer.toString();

    }
}
public class JMapConverter{

    public static DestinationClass1 toDestinationClass1(DestinationClass1 destinationClass1, SourceClass1 sourceClass1){

        JMapper<DestinationClass1, SourceClass1> mapper = 
                new JMapper<DestinationClass1, SourceClass1>(DestinationClass1.class, SourceClass1.class, ChooseConfig.DESTINATION);

        if (destinationClass1 == null){
            destinationClass1 = mapper.getDestination(sourceClass1); //always passing here
        } else {
            mapper.getDestination(destinationClass1, sourceClass1);
        }
        return destinationClass1;
    }

    public static DestinationClass2 toDestinationClass2(DestinationClass2 destinationClass2, SourceClass2 sourceClass2){

        JMapper<DestinationClass2, SourceClass2> mapper = 
                new JMapper<DestinationClass2, SourceClass2>(DestinationClass2.class, SourceClass2.class, ChooseConfig.DESTINATION);

        if (destinationClass2 == null){
            destinationClass2 = mapper.getDestination(sourceClass2);
        } else {
            mapper.getDestination(destinationClass2, sourceClass2);
        }
        return destinationClass2;
    }
}

结果是:java.lang.StackOverflowError

2 个答案:

答案 0 :(得分:0)

在源属性上使用显式转换

@JMapConversion(from={"sourceClass2"}, to={"fieldNameInDestinationClass"})
public FieldInDestClassType conversion(SourceClass2 sourceClass2){
    // do the conversion;
}

否则,框架将在尝试确定Source对象中有多少属性时崩溃。

答案 1 :(得分:0)

无法使用JMapper控制递归,您必须显式实现一个块。当您需要在同一模式下处理不同的字段时,DYNAMIC转换非常有用,在您的情况下STATIC转换就足够了。