【问题标题】:defining Unique Key in hibernate and how?在休眠中定义唯一键以及如何定义?
【发布时间】:2020-05-28 19:09:05
【问题描述】:
<many-to-one name="attachment" class="AttachmentEntity" lazy="false"
             fetch="select" cascade="delete">
    <column name="SPA_ATTACHMENT_ID" not-null="true" unique-key="IDX_AMT_COND_01"/>
</many-to-one>

唯一键的作用是什么?它将如何作为字符串工作?

【问题讨论】:

    标签: sql hibernate liquibase unique-constraint unique-key


    【解决方案1】:

    根据JBoss documentation

    唯一键属性可用于将列分组为单个, 唯一键约束。该属性覆盖任何名称 生成唯一键约束。

    unique-key 的典型用例是,您希望多个列的值作为一个整体是唯一的。

    例如:

    class Department {...}
    
    class Employee {
      Integer employeeId;
      Department department;
    }
    

    所以,为了确保两个具有相同employeeId 和department 的Employee 对象不会被持久化,我们可以使用unique-key 属性和2 列 EMP_ID 和 DEPT_ID 上的相同值 EmpIdDept对它们整体实施唯一性约束

    <property name="employeeId" column="EMP_ID" unique-key="EmpIdDept"/>
    <many-to-one name="department" column="DEPT_ID" class="Department" unique-key="EmpIdDept"/>
    

    指定为属性值的字符串,即 IDX_AMT_COND_01 在您的情况下,只是多列唯一约束的名称。

    还要检查这个answerthis one(使用@UniqueConstraint 实现相同的效果)

    注意:要使用单列唯一约束,您需要使用 独特的=“真”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      相关资源
      最近更新 更多