在休眠中定义唯一键以及如何定义?

defining Unique Key in hibernate and how?

<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>

唯一键有什么作用,它作为字符串如何工作?

根据 JBoss documentation

A unique-key attribute can be used to group columns in a single, unique key constraint. The attribute overrides the name of any generated unique key constraint.

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

例如:

class Department {...}

class Employee {
  Integer employeeId;
  Department department;
}

因此,为了确保不持久化 具有相同 employeeId 和部门 的 2 个 Employee 对象,我们可以使用 unique-key 属性具有相同值 EmpIdDept 在 2 列 EMP_ID 和 DEPT_ID 上 强制执行唯一性约束他们作为一个整体:

<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,只是多列唯一约束的名称。

还要检查这个answer and this one(使用@UniqueConstraint 实现相同)

NOTE: to use single column unique constraint, you need to use unique="true"