2016-07-25 37 views
0

我想從源類到目的地,在源中我有一個日期和在目的地LocalDateTime JMapper轉換。JMapper如何創建從日期到LocalDateTime的轉換

閱讀JMapper的文檔的邏輯方法是製作一個convertion。然而,基於來自question的解決方案的轉換不起作用,並且我總是在目標日期得到空值。

現在我的代碼

// Source Entity 
@Entity 
@Table(name = "source") 
public class Source { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column 
    private Long id;  
    @Column 
    private String name; 
    @Column 
    private Date date; 

    // ommited getters and setters 

} 
// Destination entity 
public class Destination { 
    private Long id; 
    private String name; 
    private LocalDateTime date; 

    // ommited getters and setters 
} 

// Mapping with API 
jMapperAPI = new JMapperAPI(); 
Conversion dateToLocalDateTime = conversion("dateToLocalDateTime") 
       .from("date").to("date").type(JMapConversion.Type.STATIC) 
       .body("return java.time.LocalDateTime.ofInstant(${source}.toInstant(), java.time.ZoneId.systemDefault())"); 
jMapperAPI.add(mappedClass(Destination.class).add(global() 
       .excludedAttributes("date")) 
       .add(dateToLocalDateTime)); 

// Converting 
mapper = new JMapper<>(Destination.class, Source.class, jMapperAPI); 
mapper.getDestination(source); 

回答

1

我終於找到了答案,我的問題,

首先我做了兩個錯誤,第一我認爲我必須從全局映射排除領域,但在使用convertions時不必排除它。

第二我假設JMapper爲源和目標類「date」中具有相同名稱的屬性執行「僅一種方式轉換」。

JMapper真正做的是它需要這個轉換並驗證它是否可以在從Date轉換爲LocalDateTime和從LocalDateTime轉換爲Date時應用(在我的情況下)。

鑑於我必須定義兩個轉換時從Date轉換爲LocalDateTime,另一個轉換時從LocalDateTime轉換爲Date以實現這一目的,我更改了目標字段的名稱,因爲似乎沒有其他方法可以使與皈依之間的差別應用於這種或那種方式

這是工作的代碼:

// Source Entity 
@Entity 
@Table(name = "source") 
public class Source { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column 
    private Long id;  
    @Column 
    private String name; 
    @Column 
    private Date date; 

    // ommited getters and setters 

} 
// Destination entity 
public class Destination { 
    private Long id; 
    private String name; 
    private LocalDateTime localDateTime; 

    // ommited getters and setters 
} 

// Mapping with API 
jMapperAPI = new JMapperAPI(); 

jMapperAPI.add(mappedClass(Destination.class).add(global() 
        .excludedAttributes("localDateTime")) 
        .add(attribute("localDateTime").targetAttributes("date")) 
        .add(conversion("dateToLocalDateTime") 
          .from("date").to("localDateTime") 
          .type(JMapConversion.Type.DYNAMIC) 
          .body("java.time.Instant instant = ${source}.toInstant();" + 
            "${destination.type} result = java.time.LocalDateTime.ofInstant(instant, java.time.ZoneId.systemDefault());" + 
            "return result;")) 
        .add(conversion("localDateTimeToDate") 
          .from("localDateTime").to("date") 
          .type(JMapConversion.Type.DYNAMIC) 
          .body("java.time.Instant instant = ${source}.atZone(java.time.ZoneId.systemDefault()).toInstant();" + 
            "${destination.type} result = java.util.Date.from(instant);" + 
            "return result;"))); 
+0

爲什麼排斥,包括同場?你可以通過這種方式減少配置: ... global()。targetAttributes(「date」)... – Alessandro

+0

@Alessandro無法在global()中找到targetAttributes方法() –

+0

對不起,我看錯了,在你的情況,你只能寫: ... mappedClass(Destination.class).add(global()).add(轉換(「dateToLocalDateTime」)... 沒有理由排除全球幷包括它作爲法線貼圖 – Alessandro