How to map a DTO to an existing JPA entity?

后端 未结 4 1432
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 10:49

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(SomeDT         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 11:20

    Currently ModelMapper supports mapping also to an existing object.

    It is possible to do the following (for a pseudo Spring example):

    MyEntity mye = repository.finById(id);
    ModelMapper mm = new ModelMapper();    
    mm.map(myDTO, mye);
    repository.save(mye):
    

    I am using version 2.3.1 but earlier versions might support this functionality also

提交回复
热议问题