본문 바로가기
Spring/JPA

Entity의 기본속성

by y.j 2022. 6. 5.
728x90

@Entity 

@Entity

객체를 persistence상태로 만들어준다.

 

@Id

@Id  

 자바 필드를 pk로 만들어 준다.

 

@GeneratedValue

@GeneratedValue

 자동적으로 생성할 번호를 붙여준다.

public enum GenerationType { 

    /**
     * Indicates that the persistence provider must assign 
     * primary keys for the entity using an underlying 
     * database table to ensure uniqueness.
     */
    TABLE, 

    /**
     * Indicates that the persistence provider must assign 
     * primary keys for the entity using a database sequence.
     */
    SEQUENCE, 

    /**
     * Indicates that the persistence provider must assign 
     * primary keys for the entity using a database identity column.
     */
    IDENTITY, 

    /**
     * Indicates that the persistence provider should pick an 
     * appropriate strategy for the particular database. The 
     * <code>AUTO</code> generation strategy may expect a database 
     * resource to exist, or it may attempt to create one. A vendor 
     * may provide documentation on how to create such resources 
     * in the event that it does not support schema generation 
     * or cannot create the schema resource at runtime.
     */
    AUTO
}

TABLE : 유일성이 보장된 테이블의 컬럼을 이용

IDENTITY : DB의 identity컬럼 이용

SEQUENCE : DB의 시퀀스 컬럼 이용

AUTO : DB에 맞게자동으로 선택

 

@Column

@Column

자바 필드명과 데이터베이스의 컬럼명이 다를 경우 사용한다.

 

 

@Table

@Table(name = "user", indexes = {@Index(columnList = "name")}, uniqueConstraints = {@UniqueConstraint(columnNames = "email")})

Table과 자바 객체간의 이름이 다를 경우 사용한다. index와 제약사항도 설정할 수 있다.

 

@Transient

 자바 필드에는 남겨두고 싶지만, Database와 무관하게 하고 싶을 경우 사용한다.

 

@Enumerated

@Enumerated(value = EnumType.STRING)

자바 enum을 사용 할 때 쓴다.

public enum EnumType {
    /** Persist enumerated type property or field as an integer. */
    ORDINAL,

    /** Persist enumerated type property or field as a string. */
    STRING
}

ORDINAL : enum 값들을 순서대로 번호로 사용한다.

STRING : enum 값들을 String으로 치환해서 이름 그대로 사용한다.

 

728x90

'Spring > JPA' 카테고리의 다른 글

JPA Repository  (0) 2022.06.05
Entity  (0) 2022.05.29
JPA Programming  (0) 2022.05.01

댓글