在Spring JPA中,我有一個實體,並使用FlywayDb初始化模式。 我的實體是:JPA @UniqueConstraint沒有看到列
@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唯一約束劑量它的工作註釋?! – shareef
是的,正如我所說的,在實體類中沒有註釋但是在flyway的sql文件中定義了約束,一切都OK! –
flyway中的任務是'byte'類型,但是在jpa中它的Object'Task'就是這樣!你可能需要製作'@Column(name =「task」)'和'@Column(name =「date_at」)'這可能會修復,我認爲你需要添加自加入連接...閱讀http: //stackoverflow.com/questions/15216321/jpa-self-join-using-jointable – shareef