【发布时间】:2016-02-01 17:07:40
【问题描述】:
在 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$4.perform(EntityManagerFactoryBuilderImpl.java:857)
我使用 H2 数据库。
ITask 是一个具有多个 POJO 实现的接口。 ITask 接口标注@Embeddable。
我的猜测是 JPA 尝试将唯一约束应用于 FlywayDb 库尚未创建的列。但这对我来说毫无意义。
有什么想法吗?
【问题讨论】:
-
一个问题,当您删除唯一约束的 jpa 注释时它是否有效?!
-
是的,就像我说的,在实体类中没有注释但是在flyway的sql文件中定义了约束,一切正常!
-
flyway 中的任务是
byte类型,但在 jpa 中,它的 ObjectTask是正确的!您可能需要制作@Column(name="task")和@Column(name="date_at")可能会修复,我认为您需要添加连接以进行自我连接,即...阅读stackoverflow.com/questions/15216321/…