Multiple unique constraints in JPA

Is there a way to specify using JPA that there should be multiple unique constraints on different sets of columns?

@Entity
@Table(name="person", 
       uniqueConstraints=@UniqueConstraint(columnNames={"code", "uid"}))
public class Person {
    // Unique on code and uid
    public String code;
    public String uid;

    // Unique on username
    public String username;

    public String name;
    public String email;
}

I have seen a hibernate specific annotation but I am trying to avoid vendor specific solutions as we are still deciding between hibernate and datanucleus.

Answers


The @Table's attribute uniqueConstraints actually accepts an array of these. Your example is just a shorthand for an array with a single element. Otherewise it would look like:

@Table(name="person",  uniqueConstraints={
   @UniqueConstraint(columnNames={"code", "uid"}),
   @UniqueConstraint(columnNames={"anotherField", "uid"})
})

Whenever the unique constraint is based only on one field, you can use @Column(unique=true) on that column.


Need Your Help

Why is System.Transactions TransactionScope default Isolationlevel Serializable

c# transactionscope isolation-level

I am just wondering what a good reason to use Serializable as the default Isolationlevel may be when creating a System.Transactions TransactionScope, because I cannot think of any (and it seems tha...

Can't change target platform to "any CPU"

c# .net visual-studio-2010 visual-studio build

I work on an x86 pc and use .NET4.0 (same probelem with 3.5) in VS2010. When I make a new Project (e.g. WinFormsApp), the first thing I want to do is to change the target platform of the project/so...