I have this code:
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "color"}))
@MappedSuperclass
public abstract class AbstractInstance extends Model {
@NotNull
public String name;
public String color;
...
}
but for some reason the @UniqueConstraint
has no effect - I succeed in putting in the DB multiple instances with the exact same name and color (when I query for color='green' AND name='MyName'
I get multiple results). Am I doing something wrong? Should I do something else for this compound uniqueness constraint to take effect?
Another evidence of the problem might be that when I query the DB's INFORMATION_SCHEMA.CONSTRAINTS regarding my relevant table, I get this result which doesn't seem to mention 'name' and 'color' as unique:
CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CONSTRAINT_TYPE | UNIQUE_INDEX_NAME | CHECK_EXPRESSION | COLUMN_LIST | SQL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
play | public | fkcb0e606fe2f3066d | REFERENTIAL | PRIMARY_KEY_8 | null | CONTAINER_ID | ALTER TABLE PUBLIC.UNLABELEDINSTANCE ADD
| | | | | | | CONSTRAINT PUBLIC.FKCB0E606FE2F3066D FOREIGN
| | | | | | | KEY(CONTAINER_ID) INDEX
| | | | | | | PUBLIC.FKCB0E606FE2F3066D_INDEX_C REFERENCES
| | | | | | | PUBLIC.DATASET(ID) NOCHECK
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
play | public | constraint_d7 | PRIMARY KEY | PRIMARY_KEY_C | null | ID | ALTER TABLE PUBLIC.UNLABELEDINSTANCE ADD
| | | | | | | CONSTRAINT PUBLIC.CONSTRAINT_D7 PRIMARY KEY(ID)
| | | | | | | INDEX PUBLIC.PRIMARY_KEY_C
I am using:
- Java 1.7
- Hibernate via the maven dependency: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final (I am not sure how to infer from this the version of JPA\Hibernate)
- Play Framework 1.2.7
- H2 database (the one that comes with Play Framework, for in-memory DB)