728x90
반응형
둘다 JPA 에서 Unique Key 를 정의하는 데 사용되는 애노테이션이다.
@UniqueConstraint
@Table 내에서 사용된다. 테이블 수준에서 UK 를 정의할 수 있다.
여러 개의 필드나 필드의 조합을 통해 UK 를 설정할 수 있다.
@Entity
@Table(name = "entity_table", uniqueConstraints = {
@UniqueConstraint(name = "uk_constraint_name", columnNames = {"column1", "column2"})
})
public class EntityClass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "column1")
private String column1;
@Column(name = "column2")
private String column2;
}
@Column(unique = true)
필드 수준에서 사용되며, 해당 필드의 값이 유일해야 함을 나타낸다.
이 속성을 사용하여 개별 필드의 유일성을 제약할 수 있다.
@Entity
public class EntityClass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "unique_field", unique = true)
private String uniqueField;
}
728x90
반응형
'Back-End > Spring Boot' 카테고리의 다른 글
Spring Controller header 값 가져오기 (0) | 2024.07.10 |
---|---|
데이터베이스 decimal 타입 JPA 에서 표현하기 (0) | 2024.07.09 |
JPA 적용 및 API 기능 추가하기 (0) | 2024.05.29 |
JPA를 사용하는 이유 (0) | 2024.04.16 |
Spring Boot - 서버 (0) | 2022.08.31 |
댓글