Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- concurrentsessionfilter
- Database
- 객제지향 쿼리 언어
- MySQL
- 상속관계 매핑
- spring jpa
- ddl-auto
- TABLE
- form login
- 다대다
- SessionManagementFilter
- 세션 고정 보호
- 임베디드 타입
- field column mapping
- 양방향 매핑
- 값 타입 비교
- jpa
- orphanRemovel
- 즉시 로딩
- 값 타입 컬렉션
- 지연 로딩
- 세션 생성 정책
- 기본값 타입
- Spring Security
- AnonymousAuthenticationFilter
- JPQL
- 기본 키 매핑
- Remember-me
- @MappedSuperclasss
- 동시 세션 제어
Archives
- Today
- Total
hoondev
[Spring JPA] 기본 키 매핑 본문
기본 키 매핑
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
직접할당은 @Id만 사용하면 된다. 하지만 기본키 자동생성은 몇가지 옵션들이 있다.
- GenerationType.AUTO: 방언에 따라 자동 지정, 기본값
- GenerationType.IDENTITY: 데이터 베이스에 위임
- GenerationType.SEQUENCE: 데이터베이스 시퀀스 오브젝트 사용
- GenerationType.TABLE: 키 생성용 테이블 사용, 모든 DB에서 사용
IDENTITY
commit시점에 INSERT SQL을 실행하지 않고 em.persist() 시점에 즉시 INSERT SQL 실행 하고 DB에서 식별자를 조회.
예시
@Entity
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
SEQUENCE
예시
@Entity
@SequenceGenerator(
name = “MEMBER_SEQ_GENERATOR",
sequenceName = “MEMBER_SEQ", //매핑할 데이터베이스 시퀀스 이름
initialValue = 1, allocationSize = 1)
public class Member {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "MEMBER_SEQ_GENERATOR")
private Long id;
@SequenceGenerator
name | 식별자 이름 (필수) |
sequenceName | 데이터베이스에 등록되어 있는 시퀀스 이름 (기본값: hibernate_sequence) |
initialValue | DDL 생성 시에만 사용됨, 시퀀스 DDL을 생성할 때 처음 1 시작하는 수를 지정한다 (기본값: 1) |
allocationSize | 시퀀스 한 번 호출에 증가하는 수(성능 최적화에 사용됨 데이터베이스 시퀀스 값이 하나씩 증가하도록 설정되어 있으면 이 값 을 반드시 1로 설정해야 한다 (기본값: 50) |
catalog, schema | 데이터베이스 catalog, schema 이름 |
TABLE
키 생성 전용 테이블을 하나 만들어서 데이터베이스 시퀀스를 흉 내내는 전략
키 생성 전용 테이블 생성
create table MY_SEQUENCES (
sequence_name varchar(255) not null,
next_val bigint,
primary key ( sequence_name )
)
예시
@Entity
@TableGenerator(
name = "MEMBER_SEQ_GENERATOR",
table = "MY_SEQUENCES",
pkColumnValue = “MEMBER_SEQ", allocationSize = 1)
public class Member {
@Id @GeneratedValue(strategy = GenerationType.TABLE,
generator = "MEMBER_SEQ_GENERATOR")
private Long id;
@TableGenerator
name | 식별자 생성기 이름 (필수) |
table | 키생성 테이블명 (기본값: hibernate_sequence) |
pkColumnName | 시퀀스 컬럼명 (기본값: sequence_name) |
valueColumnName | 시퀀스 값 컬럼명 (기본값: next_val) |
pkColumnValue | 키로 사용할 값 이름 (기본값: 엔티티 이름) |
initialValue | 초기 값, 마지막으로 생성된 값이 기준 (기본값: 0) |
allocationSize | 시퀀스 한 번 호출에 증가하는 수(성능 최적화에 사용됨, 기본값: 50) |
catalog, schema | 데이터베이스 catalog, schema 이름 |
uniqueConstraints(DDL) | 유니크 제약 조건을 지정할 수 있다. |
Reference
'Spring JPA' 카테고리의 다른 글
[Spring JPA] 양방향 연관관계 (0) | 2023.01.25 |
---|---|
[Spring JPA] 단방향 연관관계 (0) | 2023.01.23 |
[Spring JPA] 필드와 컬럼 매핑 (0) | 2023.01.21 |
[Spring JPA] 데이터베이스 스키마 자동 생성 (0) | 2023.01.20 |
[Spring JPA] 객체와 테이블 매핑 (0) | 2023.01.19 |
Comments