2

I'm using the @UniqueConstraint annotation on a User model:

@Table(
    name = "users",
    uniqueConstraints = @UniqueConstraint(
        columnNames = { "building_id", "pin" }
    )
)

This model is part of a Play Framework 2.7 project in Java 13.

When I want to build the project, I get an error:

[error] /app/models/common/User.java:25:27: Array constants have to be specified using the `Array(...)' factory method
[error]     uniqueConstraints = @UniqueConstraint(
[error]                           ^

According to this page, it should be correct. I don't understand this error.

2
  • 3
    "According to this page, it should be correct." - and according to the compiler, it isn't =). Try using uniqueConstraints = { @UniqueConstraint(...) }
    – Turing85
    Commented Aug 14, 2020 at 14:50
  • 1
    That was the issue :)
    – didil
    Commented Aug 14, 2020 at 14:52

1 Answer 1

0

uniqueConstraints in @Table may accept a single @UniqueConstraint annotation in some version combinations of the Play Framework and JPA, but it should accept a sequence of constraints in most recent versions. If this error emerges when updating dependencies, you can encase the @UniqueConstraint in an array.

@Table(
    // ...
    uniqueConstraints = {
        @UniqueConstraint(
            columnNames = { "building_id", "pin" }
        )
    }
)

In some cases, the @UniqueConstraint annotation can be placed on the entity class itself, instead of inside a table annotation. Support for this may be non-standard though, so double-check that it works in your environment.

@UniqueConstraint(
    columnNames = { "building_id", "pin" }
)
public class User {

Not the answer you're looking for? Browse other questions tagged or ask your own question.