| The Entity is declared as follows:
@Entity
@Table(indexes={
@Index(name="unique_name_age", columnList="name,age", unique=true)})
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id_client")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
}
The index name is set explicitly in @Index annotation. Hibernate generates schema:
Hibernate:
create table client (id_client integer not null, age integer, name varchar(255), primary key (id_client)) {elapsed: 0ms}
Hibernate:
alter table client add constraint unique_name_age unique (name, age) {elapsed: 3ms}
Here the constraint is named "unique_name_age" but not the index. Then H2 creates constraint and creates corresponding index automatically. So index name is not the one specified in @Index annotation. When the constraint is violated exception looks like this:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["UNIQUE_NAME_AGE_INDEX_7 ON PUBLIC.CLIENT(NAME, AGE) VALUES ('John', 23, 100)"; SQL statement:
I expect that index name should be the one from annotation. But I can't see the way to do so. |