JMapper 如何创建从 Date 到 LocalDateTime 的转换

JMapper how to create a convertion from Date to LocalDateTime

我正在尝试进行从 class 源到目标的 JMapper 转换,在源中我有一个日期,在目标中有一个 LocalDateTime。

阅读 JMapper 的文档,合乎逻辑的方法是使 convertion. However the covertion based on the solution from this question 不起作用,我总是在目标日期得到 null。

现在我的代码

// 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);

我终于找到了问题的答案,

首先我犯了两个错误,首先我假设我必须从全局映射中排除该字段,但在使用转换时不必排除它。

其次我假设 JMapper 对源和目标中具有相同名称的属性执行 "only one way convertion" class "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;")));