modelmapper

When saving entity converted from a DTO, hibernate throws TransientPropertyValueException: object references an unsaved transient instance"

我与影子孤独终老i 提交于 2020-06-17 10:24:43
问题 First off - I know, it might seem like the same question has been asked a million times. However, this is related rather to DTOs, not entities nor missing cascades. If I create an entity myself and save it, everything is fine. The problem occurs when I create a DTO, convert it with ModelMapper and then try to save the converted entity. If you look at the test class, the first test(saveCarByEntity) passes but the second(saveCarByDto) one produces the error. Every class connected can be seen

Java 实体-实体的映射框架

坚强是说给别人听的谎言 提交于 2020-05-08 05:55:27
<div id="content_views" class="markdown_views"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path> </svg> <h3 id="一object-mapping-的技术分类"><a name="t0"></a>一、Object mapping 的技术分类:</h3> <ul> <li><p>运行期 反射调用set/get 或者是直接对成员变量赋值 。 该方式通过invoke执行赋值 *,实现时一般会采用beanutil, Javassist等开源库。这类的代表:Dozer,ModelMaper</p></li> <li><p>编译期 动态生成set/get代码的class文件 ,在运行时直接调用该class文件。* 该方式实际上扔会存在set/get代码,只是不需要自己写了。 这类的代表:MapStruct,Selma,Orika ##

Entity To DTO Conversion for a Spring REST API

一世执手 提交于 2020-02-25 15:24:08
1. Overview In this tutorial, we'll handle the conversions that need to happen between the internal entities of a Spring application and the external DTOs (Data Transfer Objects) that are published back to the client. 2. Model Mapper Let's start by introducing the main library that we're going to use to perform this entity-DTO conversion – ModelMapper . We will need this dependency in the pom.xml : 1 2 3 4 5 < dependency > < groupId >org.modelmapper</ groupId > < artifactId >modelmapper</ artifactId > < version >2.3.5</ version > </ dependency > To check if there's any newer version of this

How to have ModelMapper.validate() succeed when using converters and providers instead of property mapping?

你说的曾经没有我的故事 提交于 2020-01-14 14:26:12
问题 Having something like: @Getter @Setter public static class Entity { private int hash; private LocalDateTime createdTime; } and @Getter @Setter public static class DTO { private String hash; private String createdTime; } I need birectional mapping so I should be able to map Entity -> DTO -> Entity . In this example the property type happens to be LocalDateTime but could be any type that needs parsing from String or so (just to say that I am not after better way to map LocalDateTime but in

How to map a DTO to an existing JPA entity?

筅森魡賤 提交于 2020-01-01 09:41:29
问题 I'm trying to map a Java DTO object to an existing JPA entity object without having to do something like the following: public MyEntity mapToMyEntity(SomeDTO dto, MyEntity entity) { entity.setField1(dto.getField1()); entity.setField2(dto.getField2()); ... entity.setField20(dto.getField20()); return entity; } Up to now I've been using ModelMapper like so: MyEntity entity = modelMapper.map(dto, SomeDTO.class); , but what I'm trying to do instead is map to an existing entity object rather than