jpa, unique in column influence another column

rura6502 :

I am using spring boot, jpa, h2.

My entity class is this.

@Entity
@Table(name="product")
public class Product {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  @Column(name="name", unique=true)
  private String name;
  private String attr1;
  private String attr2;
}

and I tried to initialize sample data using 'data.sql' in classpath. is this.

insert into product values(1, 'hong', 'attr1', 'attr2');
insert into product values(2, 'kim', 'attr1', 'attr2');

but I got the error

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/D:/OneDrive%20-%20IOChord/personal/develop/workspace/workspace_spring/jpa-00-basic/target/classes/data-h2.sql]: insert into product values(2, 'kim', 'attr1', 'attr2'); nested exception is org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "UK_JMIVYXK9RMGYSRMSQW15LQR5B_INDEX_1 ON PUBLIC.PRODUCT(NAME) VALUES ('attr2', 1)"; SQL statement:
insert into product values(2, 'kim', 'attr1', 'attr2') [23505-197]

but when I changed this

insert into product values(1, 'hong', 'attr1', 'attr2');
insert into product values(2, 'kim', 'attr3', 'attr3');

this is working. I just want to configure only one column(column 'name') is unique. but it influences another column attr1, attr2. why this is happening?

Andronicus :

The ordering of columns in database might differ from what you have in your entity, this should do it:

insert into product(id, name, attr1, attr2) values(1, 'hong', 'attr1', 'attr2');
insert into product(id, name, attr1, attr2) values(2, 'kim', 'attr1', 'attr2');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144759&siteId=1