jMapperを使用して双方向関係をマッピングする正しい方法は?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を