Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique constraint check in JPA

Tags:

hibernate

jpa

I am enforcing a unique constraint check in JPA for userid column which is enforced for all records in the user table.

@Table(name = "user",
       uniqueConstraints = @UniqueConstraint(columnNames = userid))

My requirement is that, the userid's within a particular organization needs to be unique and not across all organizations.

How do I enforce such a check?

like image 779
Joe Avatar asked Dec 03 '09 10:12

Joe


1 Answers

You can specify more than one field for your unique constraint, try:

 uniqueConstraints={@UniqueConstraint(columnNames={"userid", "organizationid"})}

By doing this, your constraint checks whether the combination of userid and organizationid is unique.

Best wishes, Fabian

like image 171
halfdan Avatar answered Sep 19 '22 09:09

halfdan