Error Log ‐ org.hibernate.PersistentObjectException: detached entity passed to persist - dnwls16071/Backend_Study_TIL GitHub Wiki

🚨 org.hibernate.PersistentObjectException: detached entity passed to persist

  • 준영속 엔티티를 영속 상태로 관리하고자 할 때 발생했다.
  • 작성한 엔티티 코드는 아래와 같았다.
@Entity
@Data
public class Product {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "product_id")
	private Long id;

	@Column(name = "name")
	private String name;

	@Column(name = "price")
	private int price;

	@Column(name = "type")
	private String type;
}
  • 여기서 기본키 생성 전략을 데이터베이스에 위임하는 전략인 IDENTITY를 사용하게 되면 필드에 직접 값을 입력하면 안 된다.
  • CSV의 파일 데이터를 읽어오기 위해서 기본키 생성 전략을 제거하고 직접 할당하는 방식으로 전환했더니 정상적으로 해결이 되었다.