Cloning JPA entity

前端 未结 8 1624
闹比i
闹比i 2020-11-29 01:55

I have a JPA entity already persisted in the database.
I would like to have a copy of it (with a different id), with some fields modified.

What is the easiest wa

8条回答
  •  余生分开走
    2020-11-29 02:19

    ModelMapper lib can be used for this purpose.

    public MyEntity clone(MyEntity myInstance) {
        MyEntity newInstance = new MyEntity();
        new ModelMapper().map(myInstance, newInstance);
        return newInstance;
    }
    

    just add the maven dependency

    
        org.modelmapper
        modelmapper
        2.3.2
    
    

提交回复
热议问题