development

최대 절전 모드 주석의 @UniqueConstraint 및 @Column (unique = true)

big-blog 2020. 9. 18. 18:48
반응형

최대 절전 모드 주석의 @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 = 1group.id = 1 , 당신과 다른 레코드를 삽입하려고하면 = 1 mask.idgroup.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.

참고URL : https://stackoverflow.com/questions/15372654/uniqueconstraint-and-columnunique-true-in-hibernate-annotation

반응형