user2488578
user2488578

Reputation: 916

UniqueConstrait on a table not working in Hibernate

Hibernate verstion is 3.x

Below is the table structure,

firstname | lastname | salary
-----------------------------------
firstname | lastname | 100
firstname | lastname | 500
firstname | lastname | 1000

Below is the java code,

@Entity
 @Table(name="TABLE_NAME",schema="SchemaName",
    uniqueConstraints={@UniqueConstraint(columnNames={"firstname","lastname"})})

Though I have mentioned the unique constraint on the tuples 'firstname' and 'lastname', am still able to add rows with the same firstname and lastname values.

I thought, I would get HibernateException or otherwise and I won't be allowed to add same firstname and lastname values for multiple rows. I don't have unique constraint created at the database level. Its there just in the code.

Why am I able to add duplicate rows? Or my understanding of unique constraint is wrong?

Upvotes: 0

Views: 98

Answers (1)

M. Deinum
M. Deinum

Reputation: 125302

Have you read the javadoc?

It is only a hint to include a unique constraint in the generated DDL. It will not do anything at runtime. Basically you need to create the constraint at the database level.

Upvotes: 2

Related Questions