Spring Data JPA - ID 생성 전략과 채번은 어떻게 되는걸까?
@Id, @GeneratedValue 동작방식을 톺아보자.
@Entity
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}@Id
package jakarta.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies the primary key of an entity.
* The field or property to which the <code>Id</code> annotation is applied
* should be one of the following types: any Java primitive type;
* any primitive wrapper type;
* <code>String</code>;
* <code>java.util.Date</code>;
* <code>java.sql.Date</code>;
* <code>java.math.BigDecimal</code>;
* <code>java.math.BigInteger</code>.
*
* <p>The mapped column for the primary key of the entity is assumed
* to be the primary key of the primary table. If no <code>Column</code> annotation
* is specified, the primary key column name is assumed to be the name
* of the primary key property or field.
*
* <pre>
* Example:
*
* @Id
* public Long getId() { return id; }
* </pre>
*
* @see Column
* @see GeneratedValue
*
* @since 1.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Id {}
@GeneratedValue
GenerationType
Hibernate의 식별자 생성 전략 내부 구현 분석



ID 생성 전략 초기화 흐름 정리
ID 채번 테스트
1. SimpleJpaRepository.save()

2. AbstractSaveEventListener.performSave()

3. AbstractSaveEventListener.performSaveOrReplicate()
4. AbstractSaveEventListener.handleGeneratedId()
5. EntityIdentityInsertAction.addInsertAction()

EntityIdentityInsertAction 에서 ID 값을 가져오는 방식
ID 채번 동작 흐름 정리
결론
Last updated