2 Replies Latest reply on May 7, 2009 9:19 PM by Shervin Asgari

    How to add uniqueConstraint on two columns together

    Shervin Asgari Master
      I have two columns that I want together to be unique validated.

      I have column username and a ManyToOne organization that together must validate unique.

      ie, if the table has these values:


      select * from table
      username = admin
      organization = Foo

      and we insert:
      username = admin
      organization = Bar

      Result should be ok. and this should fail:
      insert:
      username = admin
      organization = Foo

      But the following annotation on my Entity doesnt work
      @UniqueConstraint(columnNames = { "username", "organization_id" })
      @Entity
      public class ....

      @ManyToOne
      private Organization organization;

      @UserPrincipal
      @Column
      private String username;

      I have solved it by creating a trigger @PreUpdate and @PrePersist that manually checks it, but it would be nice to have the database constraint.
      Any help?
      PS: Sorry about formatting, but I couldnt get code block to work