본문 바로가기
JPA

[JPA] Hibernate에서 엔티티 복제하기

by palbokdev 2023. 1. 11.
MyEntity clone = entityManager.find(MyEntity.class, ID);

entityManager.detach(clone); // 기존 엔티티에 영항을 미치지 않도록 영속성 분리

clone.setId(null); // 기존 엔티티와 아이디가 겹치지 않도록 null로 변경
clone.setXXX(XXX); // 다른 속성값 변경 가능

entityManager.persist(clone); // 엔티티가 생성되도록 영속성 연결

 

ref : https://stackoverflow.com/questions/9944882/how-to-deep-copy-a-hibernate-entity-while-using-a-newly-generated-entity-identif