我在使用H2和Spring Boot 2.5.0时,当Spring Boot尝试在表中插入数据时,遇到了错误。似乎Spring没有创建表。
这是我的实体:
这是我的实体:
@Entity(name = "Users")
@Table(name = "users")
public class UserEntity implements Serializable {
// Serial
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String surname;
private String email;
// Getters and setters
}
我的代码库
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
}
我已经添加了h2
和spring-boot-starter-data-jpa
到我的pom.xml文件中。
当我以调试模式启动服务器时,我遇到了以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/PATH/target/classes/data.sql]: INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', 'mitchell.hudson11@example.com'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabla "USERS" no encontrada
Table "USERS" not found; SQL statement:
INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', 'mitchell.hudson11@example.com') [42102-200]
这是我的 data.sql 文件
INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', 'mitchell.hudson11@example.com');
INSERT INTO users (id, name, surname, email) VALUES (2, 'Melanie', 'Bell', 'melanie.bell51@example.com');
INSERT INTO users (id, name, surname, email) VALUES (3, 'Diane', 'Ruiz', 'diane.ruiz24@example.com');
我尝试在属性文件中进行一些配置,但是我无法解决错误。
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driver-class-name=org.h2.Driver
我可以看到插入数据的查询,但它没有生成创建表格的查询,所以我认为问题就出在那里。
提前致谢。