gpt4 book ai didi

java - SpringBoot JPA data.sql失败

转载 作者:行者123 更新时间:2023-12-02 08:50:42 25 4
gpt4 key购买 nike

我想要 sql 脚本来加载我的数据。但我的 SQL 脚本不起作用。我不知道为什么会导致错误。

我的data.sql是

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

我的测试代码是

 @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");
}

我的域 Person 类是

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

}

我的错误是

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

我已经研究这个错误问题很长时间了。但我没有发现。请帮助我

最佳答案

data.sql代码

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

必须改变

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

关于java - SpringBoot JPA data.sql失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com