JPA @UniqueConstraint 看不到列

JPA @UniqueConstraint doesn't see the columns

在 Spring JPA 中,我有一个实体,我使用 FlywayDb 初始化了模式。 我的实体是:

@Entity
@Table(schema = "scheduler",
        uniqueConstraints={@UniqueConstraint(name = "uq_task", columnNames = {"task", "date_at"})}
)
public class Task {
    @Id
    private Long id;

    @Embedded
    @Column(nullable = false)
    private ITask task;

    @Column(nullable = false)
    private Date dateAt;

}

架构初始化如下:

CREATE SCHEMA scheduler;

CREATE TABLE scheduler.task (
    id bigserial primary key,
    task bytea NOT NULL,
    date_at timestamp NOT NULL
);

CREATE UNIQUE INDEX uq_task
  ON scheduler.task(task, date_at);

没有对实体的约束,它可以工作,没有。特别是我有例外:

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (task, date_at) on table task: database column 'task' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
    at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1684)
    at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1616)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1452)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:857)

我用的是H2数据库。

ITask 是一个具有多个 POJO 实现的接口。 ITask 接口用 @Embeddable 注释。

我的猜测是 JPA 试图对 FlywayDb 库尚未创建的列应用唯一约束。但这对我来说毫无意义。

有什么想法吗?

现在更新您的问题后,我猜您的 ITask 界面中的属性有问题,请阅读 that 文档。在我看来,您必须覆盖可嵌入的实体属性才能解决您的问题。