2017-02-11 1 views
1

Wie ist der richtige Weg, bidirektionale Beziehungen mit jMapper abzubilden?JMapper - Richtiger Weg, bidirektionale Beziehung abzubilden

Ich versuche, @JMapConversion Annotation zu verwenden, aber ich kann die Endlosschleife nicht stoppen.

Hier ist mein Code:

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; 
    } 
} 

Das Ergebnis ist: java.lang.StackOverflowError

Antwort

0

eine explizite Konvertierung auf den Quelleigenschaften Verwendung:

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

Ansonsten ist der Rahmen wird beim Versuch, herauszufinden, wie viele Eigenschaften sich im Source-Objekt befinden, abstürzen.

0

Es gibt keine Möglichkeit, die Rekursion mit JMapper zu steuern, Sie müssen einen Block explizit implementieren. DYNAMIC Konvertierung ist nützlich, wenn Sie verschiedene Felder im gleichen Modus behandeln müssen, in Ihrem Fall STATIC Konvertierung ist ausreichend.

Verwandte Themen