@UniqueConstraint and @Column(unique = true) in hibernate annotation

前端 未结 4 1836
别跟我提以往
别跟我提以往 2020-12-07 10:04

What is difference between @UniqueConstraint and @Column(unique = true)?

For example:

@Table(
   name = \"product_s         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 11:01

    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.

提交回复
热议问题