Can MapStruct do a deep deproxy of Hibernate Entity Classes

六月ゝ 毕业季﹏ 提交于 2019-12-06 08:33:35

Yes you can do that with MapStruct. However, only by explicitly marking what you want to map and what you want to ignore.

Let's say you have this classes:

public class Car {

    private String name;
    private int year;
    //This is lazy loaded
    private List<Wheel> wheels;
    //getters and setters omitted for simplicity
}

public class Wheel {
    private boolean front;
    private boolean right;
    //getters and setters omitted for simplicity
}

You will need a mapper that looks like this:

@Mapper
public interface CarMapper {

    @Mapping(target="wheels", ignore=true)
    Car mapWithoutWheels(Car car);

    Car mapWithWheels(Car car);

    List<Wheel> map(List<Wheel> wheels);

    Wheel map(Wheel wheel);
}

The explicit mapping for List<Wheel> and Wheel is needed if you want to force MapStruct to create new objects and not do direct mapping. Currently, if MapStruct sees that the source and target type are same it does direct assignment (with lists it will create a new list, but it won't call getters in the elements of the list).

If Wheel had some lazy loaded elements, then you can have 2 methods for mapping Wheel and you will have to use selection based on qualifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!