Spring Boot集成H2内存数据库提示表不存在(数据库为空)问题
排查Spring Boot+H2数据库"Table 'A' not found"异常原因
项目环境
使用IntelliJ Community Edition创建Spring Boot项目,集成内存H2数据库。
POM.xml依赖配置
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
实体类代码
@Entity @Table(name = "A") class Coffee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private String id; @Column(nullable = false,columnDefinition = "TEXT") private String name; public A(String name){ this.name = name; } public A() { } public String getId(){ return id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public void setId(String id){ this.id = id; } }
Repository与REST控制器代码
@Repository interface ARepository extends CrudRepository<A,String> {} @RestController @RequestMapping("/initial") class DBSample { @Autowired private final ARepository zrepository; RESTApiDBController(ARepository zRepository) { this.zRepository = zRepository; this.zRepository.saveAll(new A("a"),new A("b"), new A("c")); } }
异常信息
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement [Table "A" not found (this database is empty); SQL statement: insert into a (name,id) values (?,default) [42104-224]] [insert into a (name,id) values (?,default)]
application.properties配置
spring.application.name=database # H2 database configuration spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= # Enable automatic table creation spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect # H2 console (optional, for manual inspection) spring.h2.console.enabled=true spring.h2.console.path=/h2-console
问题原因与解决方案
1. 实体类名称与代码引用不匹配
实体类实际类名为Coffee,但代码中多处错误用A替代:构造函数定义为public A(String name)、Repository泛型写为CrudRepository<A,String>、控制器中实例化new A("a")。JPA无法识别不存在的A实体,因此不会自动生成表A。
- 修复:将所有错误引用的
A替换为实际类名Coffee,覆盖构造函数、Repository泛型、对象实例化所有场景。
2. 实体类访问修饰符缺失
当前Coffee类使用默认包私有修饰符,Spring JPA要求实体类为public才能被正确扫描识别,否则无法生成对应数据库表。
- 修复:给
Coffee类添加public修饰符。
3. 控制器构造函数名称错误
控制器类名为DBSample,但构造函数定义为RESTApiDBController(ARepository zRepository),这会引发编译错误,直接影响Bean初始化和数据插入逻辑。
- 修复:将构造函数名称改为与类名一致的
DBSample(ARepository zRepository)。
4. saveAll方法参数格式错误
CrudRepository.saveAll()要求参数为Iterable类型,直接传入多个对象会编译失败,需将对象包装为List。
- 修复:用
List.of()或Arrays.asList()包装对象,示例:this.coffeeRepository.saveAll(List.of(new Coffee("a"), new Coffee("b"), new Coffee("c")));
5. 构造注入冗余注解与字段名不一致
private final字段用构造注入时,Spring 4.3+无需添加@Autowired注解;且字段名zrepository与构造函数参数zRepository拼写不一致,会导致赋值错误。
- 修复:移除
@Autowired注解,确保字段名与构造函数参数名一致。
内容的提问来源于stack exchange,提问作者Saragadam Sujay
相关产品推荐
相关产品推荐

