modelmapper

Better way to map Kotlin data objects to data objects

一个人想着一个人 提交于 2019-12-29 10:35:10
问题 I want to convert/map some "data" class objects to similar "data" class objects. For example, classes for web form to classes for database records. data class PersonForm( val firstName: String, val lastName: String, val age: Int, // maybe many fields exist here like address, card number, etc. val tel: String ) // maps to ... data class PersonRecord( val name: String, // "${firstName} ${lastName}" val age: Int, // copy of age // maybe many fields exist here like address, card number, etc. val

ModelMapper not ignoring null values

≡放荡痞女 提交于 2019-12-25 01:15:35
问题 I want to do a partial update on one of my entities but if one propertie is null then the entity to be updated gets that value set to null too. I want that if a property from the source is null then to keep the one from the source. I have tried this but no luck: @Bean public ModelMapper modelMapper() { ModelMapper modelMapper = new ModelMapper(); modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull()); modelMapper.createTypeMap(String.class, Date.class); modelMapper

ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

对着背影说爱祢 提交于 2019-12-24 05:10:16
问题 I've been using modelmapper and java 8 Optionals all around the application which was working fine because they were primitive types; until I changed one of my model objects' field to Optional type. Then all hell broke loose. Turns out many libraries cannot handle generics very well. Here is the structure public class MyObjectDto { private Optional<MySubObjectDto> mySubObject; } public MyObject { privae Optional<MySubjObject> mySubObject; } When I attempt to map MyObjectDto to MyObject ,

Modelmapper to convert from String to LocalDate

给你一囗甜甜゛ 提交于 2019-12-22 07:18:46
问题 My DTO is having date field in String format. My entity is having date as LocalDate. Currently I am skipping it from map and then later manually explicitly setting it (String to Date and vis-versa). is it possible to convert it automatically? I tried Converter inside spring bean but it gives me lot of compile errors (type Converter does not take parameters, does not override convert method - also lot of error for convert() as well). @Bean public ModelMapper studentModelMapper() { ....

ModelMapper: mapping abstract classes during runtime

喜夏-厌秋 提交于 2019-12-19 09:18:25
问题 I am using ModelMapper Framework (http://modelmapper.org/) for mapping objects in Java. I have encountered a problem while mapping concrete classes (DTO to Entites) containing abstract classes. Example: Task has a list of AbstractItems. AbstractItems are Question and Criteria. public class TaskDTO { ... private List<AbstractItemDTO> items; } Mapping method: // task is an TaskDTO object return getModelMapper().map(task, TaskEntity.class); ModelMapper tries to create a new instance of

ModelMapper, mapping list of Entites to List of DTO objects

我怕爱的太早我们不能终老 提交于 2019-12-18 12:35:16
问题 I am writing simple blog web application using Spring MVC framework. I am willing to add DTO layer to my app. I decided to use ModelMapper framework for conversion from Entity objects to DTO objects used in my views. I have just one problem. On my main page, I am showing a list of posts on my blog. In my view, it's just list of Post (Entity) objects. I want to change it to pass a list of PostDTO objects to my view. Is there any way to map List of Post objects to List of PostDTO object with

Bean 字段复制利器 MapStruct

蹲街弑〆低调 提交于 2019-12-16 11:33:02
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 本文聊一个工具类,MapStruct ,它是一个在 dto,po(do/entity),vo 等这些 pojo 中转换字段的一个工具,在应用中经常有这样的转换,在 spring 和 apache-commons 中也有 BeanUtils 复制,但不够灵活, 我之前也用反射写过一个字段复制 ,这种复制一般来是是很大量的,用反射会使系统的性能降低,有一种更高效的办法就是使用字节码工具类生成这些代码,这时在编译期就把代码弄好了,并且出错也好调试。 除了 MapStruct ,类似的工具还有 Spring 的 BeanUtils ,apachecommons 的 BeanUtils Dozer - Mapper that copies data from one object to another using annotations and API or XML configuration. JMapper - Uses byte code manipulation for lightning-fast mapping. Supports annotations and API or XML configuration. ModelMapper - Intelligent object mapping library

ModelMapper: Choose mapping based on Child class

一曲冷凌霜 提交于 2019-12-12 10:34:44
问题 TL;DR I want to use modelMapper in a way that I map from AbstractParent to AbstractParentDTO and later in the ModelMapper-Config call the specific mappers for each Sub-class and then skip the rest of the (abstrac-class) mappings. How is that Possible? Is this the right approach? Is there a design flaw? What I have: The parent entity: @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "type") public abstract class Parent { //some more fields } One child entity: //Basic

ClassCastException in ModelMapper: EnhancerByModelMapper cannot be cast

假装没事ソ 提交于 2019-12-11 03:44:02
问题 I'm using ModelMapper 0.7.4 (latest version) in a Play 2.4.2 (latest version) framework application. Play 2.4 has an internal Google Guice dependency injection solution build into it, and our application is a manually bridged from Guice to a Spring Framework dependency injection solution, to get Play 2.4 to work with Spring. So communication flows from Play to Guice to Spring. Things (dependency injection with Spring) seem to work fine, but when a random Java class is changed in the test

ModelMapper skip a field

萝らか妹 提交于 2019-12-07 03:50:39
问题 I would like to map between UserDTO and User , but excluding one field, say city . How can I do that, cause I though that this approach would work, but it doesn't: ModelMapper modelMapper = new ModelMapper(); modelMapper.typeMap(UserDTO.class,User.class).addMappings(mp -> { mp.skip(User::setCity); }); 回答1: Because of the generic parameters, we couldn't use the lambda expression. ModelMapper modelMapper = new ModelMapper(); modelMapper.addMappings(new PropertyMap<Dto, Source>() { @Override