in our company have discovered dozer has problems copying fields hibernate entity simple dto (a simple pojo) fields, maps of other entities. how looks:
class myjpaentity{ @jpamappings(fetchtype.eager) map<integer, embeddablejpaentity> map; } class mydto{ map<integer, myotherdto> map; } soo, dozer, when trying copy fields entity dto tries somehow copy proxy not contents, raising lazyinitializationexception (even though fetch type eager). figured out, can create wrapper, , custom converter deals situation:
public class maybepersistentmaptohashmapconverter extends dozerconverter<map, map> implements mapperaware { public static class wrapper { private map map; public wrapper() { } public wrapper(map map) { this.map = map; } public map getmap() { return map; } public void setmap(map map) { this.map = map; } } private mapper mapper; public maybepersistentmaptohashmapconverter() { super(map.class, map.class); } @override public void setmapper(mapper mapper) { this.mapper = mapper; } @override public map convertto(map source, map destination) { return map(source, destination); } @override public map convertfrom(map source, map destination) { return map(source, destination); } private map map(map source, map destination) { wrapper wrapper; if (source instanceof persistentmap) { wrapper = new wrapper(new hashmap<>(source)); } else { wrapper = new wrapper(source); } if (destination == null) { wrapper map = mapper.map(wrapper, wrapper.class); return map.map; } else { throw new unsupportedoperationexception(); } } } so stuff, wraps map dozer not copy proxymap. configure mapping in code:
mapping(fromtype, totype, oneway()) .fields("map", "map", fieldsmappingoptions.customconverter(maybepersistentmaptohashmapconverter.class) so far good. so problem? problem other way mapping - dto entity. instead of copying mydto myjpaentity entries entity (with mapping inner dto inner entity, copies myjpaentity map entries - cannot guess target type, , instead of mapping objects, copies them.
so able solve problem well, specifying hint map types:
fields("map", "map", fieldsmappingoptions.hinta(myotherdto.class), fieldsmappingoptions.hintb(embeddablejpaentity.class) ) i helped people allready ;) , happy solved problems.
until had copy values entity entity using doser. now, if use hints, lazyinitexception, if use wrapping converter above hints ignored mapper. , sourceforge docs dozer inaccessible right now. have idea how use mapper , force use hints? or how configure mappings builder?
i used mapper in maybepersistentmaptohashmapconverter not aware should use hints, unaware mapping field of name "map"
in our project solved problem , stuck badly in problem solved 1 ;).
your lazyinitializationexception maybe calling .class in map method dozer. that's because dozer instantiate new entity , fill , entity not in current session. change our dozer map method calling from:
a_entity newentity = dozer.map(dto,a_entity.class); to:
a_entity newentity = new a_entity(); dozer.map(a_dto,newentity); and in other way:
a_dto dto = dozer.map(entity,a_dto.class); to:
a_dto dto = spring.context.getbean("a_dto"); dozer.map(entity,dto); i hope usefull.
Comments
Post a Comment