최대 절전 모드 주석의 @UniqueConstraint 및 @Column (unique = true)
@UniqueConstraint 와 @Column (unique = true)의 차이점은 무엇입니까 ?
예를 들면 :
@Table(
name = "product_serial_group_mask",
uniqueConstraints = {@UniqueConstraint(columnNames = {"mask", "group"})}
)
과
@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private ProductSerialMask mask;
@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Group group;
앞서 말했듯 이는 단일 필드 일 때 @Column(unique = true)
의 지름길 UniqueConstraint
입니다.
당신이 준 예에서, 둘 사이에는 큰 차이가 있습니다.
@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private ProductSerialMask mask;
@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Group group;
이 코드는 mask
및 둘 다 group
고유해야하지만 별도로 있어야 함을 의미합니다 . 예를 들어, 당신이 가진 기록을 가지고있는 경우 것이 그 수단 mask.id = 1 과 다른 레코드 삽입하고 시도 mask.id = 1을 그 열이 고유 한 값을 가져야하기 때문에, 당신은 오류가 발생합니다. 그룹에도 동일하게 적용됩니다.
반면에
@Table(
name = "product_serial_group_mask",
uniqueConstraints = {@UniqueConstraint(columnNames = {"mask", "group"})}
)
결합 된 마스크 + 그룹의 값이 고유해야 함을 의미합니다. 당신은 할 수있는 것을 의미한다, 예를 들어,와 기록 mask.id = 1 및 group.id = 1 , 당신과 다른 레코드를 삽입하려고하면 = 1 mask.id 및 group.id = 2 , 그것은 삽입됩니다 첫 번째 경우에는 그렇지 않습니다.
마스크와 그룹을 개별적으로 그리고 클래스 수준에서 고유하게하려면 다음과 같이 코드를 작성해야합니다.
@Table(
name = "product_serial_group_mask",
uniqueConstraints = {
@UniqueConstraint(columnNames = "mask"),
@UniqueConstraint(columnNames = "group")
}
)
이것은 첫 번째 코드 블록과 동일한 효과가 있습니다.
Java EE 문서에서 :
public abstract boolean unique
(Optional) Whether the property is a unique key. This is a shortcut for the UniqueConstraint annotation at the table level and is useful for when the unique key constraint is only a single field. This constraint applies in addition to any constraint entailed by primary key mapping and to constraints specified at the table level.
See doc
In addition to Boaz's answer ....
@UniqueConstraint
allows you to name the constraint, while @Column(unique = true)
generates a random name (e.g. UK_3u5h7y36qqa13y3mauc5xxayq
).
Sometimes it can be helpful to know what table a constraint is associated with. E.g.:
@Table(
name = "product_serial_group_mask",
uniqueConstraints = {
@UniqueConstraint(
columnNames = {"mask", "group"},
name="uk_product_serial_group_mask"
)
}
)
In addition to @Boaz's and @vegemite4me's answers....
By implementing ImplicitNamingStrategy
you may create rules for automatically naming the constraints. Note you add your naming strategy to the metadataBuilder
during Hibernate's initialization:
metadataBuilder.applyImplicitNamingStrategy(new MyImplicitNamingStrategy());
It works for @UniqueConstraint
, but not for @Column(unique = true)
, which always generates a random name (e.g. UK_3u5h7y36qqa13y3mauc5xxayq).
There is a bug report to solve this issue, so if you can, please vote there to have this implemented. Here: https://hibernate.atlassian.net/browse/HHH-11586
Thanks.
'development' 카테고리의 다른 글
Android 조각. (0) | 2020.09.18 |
---|---|
Meteor 프로젝트에서 기존 MongoDB를 어떻게 사용합니까? (0) | 2020.09.18 |
Android (9) Pie에서 모든 네트워크 연결 유형 HTTP 및 HTTPS를 허용하는 방법은 무엇입니까? (0) | 2020.09.18 |
MsSQL에서 간단한 '찾기 및 바꾸기'를 수행하려면 어떻게합니까? (0) | 2020.09.18 |
Javascript에서 정규식 일치 수 계산 (0) | 2020.09.18 |