Chcę skrypt sql, aby załadować moje dane. ale mój skrypt SQL nie działa. i nie wiem, dlaczego spowodowany jest błąd.

Moje data.sql to

insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A');

A mój kod testowy to

 @Test
void curd() {
    Person person = new Person();
    person.setName("john");
    person.setAge(10);
    person.setBloodType("A");

    personRepository.save(person);

    List<Person> result = personRepository.findByName("john");

    assertThat(result.size()).isEqualTo(1);
    assertThat(result.get(0).getName()).isEqualTo("john");
    assertThat(result.get(0).getAge()).isEqualTo(10);
    assertThat(result.get(0).getBloodType()).isEqualTo("A");
}

A moja klasa Person domeny to

@Entity
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Data
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NonNull
private String name;

@NonNull
private int age;

private String hobby;

@NonNull
private String bloodType;

@Valid
@Embedded
private Birthday birthday;

private String job;

@ToString.Exclude
private String phoneNumber;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private Block block;

}

A mój błąd to

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/D:/CCC/intellij_ex/mycontact/build/resources/test/data.sql]: insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO PERSON('id'[*], 'name', 'age', 'blood_type') VALUES (1, 'martin', 10, 'A')"; expected "identifier"; SQL statement:
insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A') 

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/D:/CCC/intellij_ex/mycontact/build/resources/test/data.sql]: insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO PERSON('id'[*], 'name', 'age', 'blood_type') VALUES (1, 'martin', 10, 'A')"; expected "identifier"; SQL statement:
insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A') 

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO PERSON('id'[*], 'name', 'age', 'blood_type') VALUES (1, 'martin', 10, 'A')"; expected "identifier"; SQL statement:
insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A')

Tak długo wykopuję ten błąd. ale nie dowiedziałem się. proszę, pomóż mi

0
Jongwon 22 marzec 2020, 13:13

2 odpowiedzi

Najlepsza odpowiedź

Kod data.sql

insert into person('id', 'name', 'age', 'blood_type') values (1, 'martin', 10, 'A');

Muszą się zmienić

insert into person(`id`, `name`, `age`, `blood_type`) values (1, 'martin', 10);
1
Jongwon 22 marzec 2020, 11:57

Twój identyfikator został już wygenerowany, jak poniżej.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

Więc możesz spróbować ponownie zmodyfikowane zapytanie SQL.

insert into person('name', 'age', 'blood_type') values ('martin', 10, 'A');
0
Rohan C Bandara 22 marzec 2020, 12:12