java - JMapper how to create a convertion from Date to LocalDateTime -
i'm trying make jmapper convertion class source destination, in source have date , in destination localdatetime.
reading docs of jmapper logical way make convertion. covertion based on solution question doesn't work , null on destination date.
now code
// 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 , setters } // destination entity public class destination { private long id; private string name; private localdatetime date; // ommited getters , setters } // mapping 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);
finally found answer on question,
first making 2 mistakes, first assumed had exclude field global mapping doesn't have excluded when using convertions.
and second assuming jmapper "only 1 way convertion" attribute same name in source , in destination class "date".
what jmapper takes convertion , validates if can applied when converting (in case) date localdatetime , localdatetime date.
given had define 2 convertions 1 when converting date localdatetime , when converting localdatetime date achieve first changed name of destination field, because seems there no other way make difference between convertion applied 1 way or another
this code works:
// 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 , setters } // destination entity public class destination { private long id; private string name; private localdatetime localdatetime; // ommited getters , setters } // mapping 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;")));
Comments
Post a Comment