javajpaplayframeworkebean

@UniqueConstraint: Array constants have to be specified using the `Array(...)' factory method


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.


Solution

  • 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 {