0

In Spring JPA I havean entity and I init the schema using FlywayDb. My entity is:

@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;

}

The schema is initialized as follows:

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);

Without the constraints on the entity, it works, with it doesn't. In particular I have the exception:

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)

I use an H2 database.

ITask is an interface with several POJO implementations. ITask interface is annotated with @Embeddable.

My guess is that JPA tries to apply the unique constraints on columns that are not yet created by FlywayDb library. But this makes no sense to me.

Any idea?

mat_boy
  • 12,998
  • 22
  • 72
  • 116
  • one question when you remove the jpa annotation of unique constraint dose it work ?! – shareef Nov 01 '15 at 20:56
  • Yes, as I said, without annotation in the entity class but with the constraints defined in the sql file for flyway, everything is ok! – mat_boy Nov 01 '15 at 20:57
  • task in flyway is of type `byte` but in jpa its Object `Task` is that even righ ! you may need to make `@Column(name="task")` and `@Column(name="date_at")` that may fixes and i think you need to add join for self join that is ... read http://stackoverflow.com/questions/15216321/jpa-self-join-using-jointable – shareef Nov 01 '15 at 21:13

1 Answers1

1

After update of you question now I can guess that there is a problem with attribute in your ITask insterface please read that doc. In my opinion you have to override embbedable entity attribute to fix your problems.

burovmarley
  • 644
  • 3
  • 8