映射双向关系的正确方法

映射双向关系的正确方法

如何使用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

最佳答案

在源属性上使用显式转换:

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


否则,该框架将在尝试找出Source对象中有多少个属性时崩溃。

08-03 14:15