skill/Java.Kotlin

JPA 에러 JpaSystemException, IdentifierGenerationException

have a nice day :D 2024. 1. 12. 10:25
반응형

에러 발생 시,
org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save():

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():

Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():


JPA @Id가 pk=auto increment 일 경우,
@Id 어노테이션에 @GeneratedValue(strategy= GenerationType.IDENTITY) 추가!

import lombok.Getter
import lombok.Setter
import java.time.LocalDateTime
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Getter
@Setter
@Entity
data class YOUR_TABLE (
                   @Id
                   @GeneratedValue(strategy= GenerationType.IDENTITY)
                   var seq: Int?, // seq (auto increment)
                   var creDttm: LocalDateTime // 생성 일
)


@Id 어노테이션은 JPA에서 엔티티 클래스의 기본 키를 지정하는 데 사용 된다.
기본 키는 엔티티를 식별하는데 사용되며, 각 엔티티는 반드시 하나의 기본 키를 가져야 한다.

@GeneratedValue 어노테이션은 기본 키의 값을 어떻게 생성할지 지정하고, GenerationType.IDENTITY 전략을 사용하여 데이터베이서의 자동 증가 기능을 활용하여 기본 키를 생성하도록 설정 되어 있다.

엔티티를 데이터베이스에 저장할 때, JPA는 자동으로 데이터베이스에 기본 키 값을 생성하고 할당 하고, 각 데이터베이스에 알맞게 처리한다. (개발자가 기본 키 생성에 대한 별도의 로직 작성 필요 없음)

참고
https://chat.openai.com/

반응형