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

What is the Unique Key doing and how will it work as a string?

htshame
  • 6,599
  • 5
  • 36
  • 56

1 Answers1

1

As per the 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.

Typical use case for unique-key would be, when you want the values of multiple columns as a whole to be unique.

For example:

class Department {...}

class Employee {
  Integer employeeId;
  Department department;
}

So, to ensure that 2 Employee objects with same employeeId and department are not persisted, we can use the unique-key attribute with same value EmpIdDept on the 2 columns EMP_ID and DEPT_ID to enforce the uniqueness constraint on them as a whole:

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

The string specified as the attribute value, i.e. IDX_AMT_COND_01 in your case, is just the name of the multi column unique constraint.

Also check this answer and this one (to achieve the same using @UniqueConstraint)

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

Chandan Hegde
  • 311
  • 3
  • 8