본문 바로가기
Spring/JPA

복수의 유일키 제약조건 지정하는 방법

by YellowCow 2023. 5. 26.

문법

@Table(
    name="테이블 이름",
    
    uniqueConstraints={
        @UniqueConstraint(
             name = "unique 제약조건 이름",
             columnNames = {
                "포함할 컬럼이름 1",
                "포함할 컬럼이름 2"
            }
      ),
}
public class Entity{

}

 

예제

@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Table(name="fcm_device_token",
        uniqueConstraints= {
                @UniqueConstraint(
                        name = "fcm_device_token_device_token_uindex",
                        columnNames = {
                                "user_id",
                                "device_token"
                        }
                )
        }
)
@Entity
public class FcmDeviceToken {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private User user;

    @Column(name="device_token")
    private String deviceToken;
}

 

*참고자료

https://ttl-blog.tistory.com/1176

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

@MappedSuperclass  (0) 2023.03.24
상속관계 매핑  (0) 2023.03.24
연관관계 - @ManyToMany  (0) 2023.03.23
연관관계 - @OneToOne  (0) 2023.03.22
연관관계 - @OneToMany  (0) 2023.03.18

댓글