Zerosero
Zerosero

Reputation: 343

Hibernate mapping of multiple columns constraint

Can you guys tell me how to map a combination of columns as a constraint in hbm? I performed query below in database but got no idea how it should be done in my hbm.xml class

    ALTER TABLE USERS add CONSTRAINT NODUPLICATE UNIQUE (NAME, AGE, STATUS);

Do I just put unique = true

    <property column="NAME" name="name" type="string" unique="true"/>

in every field?

Thanks in advance!

Upvotes: 2

Views: 1062

Answers (2)

Igor Trześniewski
Igor Trześniewski

Reputation: 1

Maybe it is fine. It is from doc gave by Zeromus:

<column name="column_name" length="N" precision="N" scale="N" not-null="true|false" unique="true|false" unique-key="multicolumn_unique_key_name" index="index_name" sql-type="sql_type_name" check="SQL expression" default="SQL expression"/>

Upvotes: 0

Zeromus
Zeromus

Reputation: 4532

From Hibernate docs:

The element allows the definition of a named, logical grouping of the properties of a class. The most important use of the construct is that it allows a combination of properties to be the target of a property-ref. It is also a convenient way to define a multi-column unique constraint.

This means you can nest multiple property inside a properties tag

<properties unique="true">
    <property column="NAME" name="name" type="string"/>
    //your others properties for the uniqueness
</properties>

Upvotes: 1

Related Questions