繁体   English   中英

H2 错误,未使用我的 data.sql 文件创建模式

[英]Error with H2 not creating the schema with my data.sql file

我有这个项目,我刚刚开始并使用我老师的 model 我创建了一个简单的 class 以映射到 H2 中。 到目前为止没有问题我运行应用程序并生成了表并且我尝试了一些插入命令并且它们很好但是当我在资源文件夹中添加data.sql时项目拒绝生成模式。

这是我的 application.properties 文件:

# DATABASE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.username=sa
spring.datasource.password=
    
# JPA
spring.jpa.hibernate.ddl-auto=update

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

超级英雄.class:

@Entity
@Table(name = "super_hero")
@Data
public class SuperHero {

@Id
@Column(name="id", updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false, columnDefinition = "TEXT")
private String name;
}

data.sql 文件:

INSERT INTO super_hero(id, name) VALUES (1, 'Super Man');

这是错误:

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:/C:/Users/gabri/git/plexus-super-heroes/target/classes/data.sql]: INSERT 
INTO SUPER_HERO(id, name) VALUES (1, 'Super Man'); nested exception is 
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "SUPER_HERO" not found; SQL statement:
INSERT INTO SUPER_HERO(id, name) VALUES (1, 'Super Man') [42102-200]

我尝试将表名更改为小写和大写,从头开始重新创建项目,但一直出现此错误。

您可以将您的语句放入 import.sql 而不是 data.sql 并重试。 Hibernate 使用这个文件来初始化表。

非常感谢。 重命名文件后,它工作得很好。 我仍然不明白为什么在另一个具有相同 pom 依赖项的项目中,只有一个是针对 java 1.8 和我的 java 11 的。

对我来说也 import.sql 工作正常,我尝试使用 data.sql 进行更多实验,我认为对于内存数据库模型更好地使用 import.sql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM