Feras Odeh Feras Odeh - 2 years ago 135
Java Question

Using unique constraint on Hibernate JPA2

How can I implement my unique constraints on the hibernate POJO's? assuming the database doesn't contain any.

I have seen the unique attribute in

annotation but I couldn't get it to work?

What if I want to apply this constraint to more than one column?

Answer Source

Bascially, you cannot implement unique constraint without database support.

@UniqueConstraint and unique attribute of @Column are instructions for schema generation tool to generate the corresponsing constraints, they don't implement constraints itself.

You can do some kind of manual checking before inserting new entities, but in this case you should be aware of possible problems with concurrent transactions.

Therefore applying constraints in the database is the preferred choice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download