Spring Boot没有自动创建表格

7
我在使用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> {

}

我已经添加了h2spring-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

我可以看到插入数据的查询,但它没有生成创建表格的查询,所以我认为问题就出在那里。

提前致谢。


你是否定义了一个继承自JPARepository的仓库接口? - J Asgarov
@JAsgarov 是的,我已将其添加到主帖中。我有另一个项目,使用Spring Boot 2.3.0.RELEASE并且它可以正常工作,但是我认为在配置项目的方式上发生了一些变化,我可能遗漏了某些内容。 - cjgmj
3个回答

30

在Spring Boot 2.5中,SQL脚本数据源初始化功能已经进行了重新设计。

默认情况下,现在会在Hibernate初始化之前运行data.sql脚本。这使得基于脚本的初始化行为与Flyway和Liquibase保持一致。如果您想使用data.sql来填充由Hibernate创建的模式,请将spring.jpa.defer-datasource-initialization设置为true。虽然不推荐混合使用数据库初始化技术,但这也允许您使用schema.sql脚本在通过data.sql填充之前构建基于Hibernate创建的模式。

参考资料

Spring Boot 2.5发布说明 - Hibernate和data.sql


1
我几分钟前找到了它,本来想评论一下,但你比我更快。这解决了问题!我在这里找到了它。 - cjgmj
我感谢这个(原始问题)和这个答案来强调这个问题。我已经被“重设计”Spring Data黑魔法巫术的更改疲惫不堪了。10.9.2.使用Hibernate初始化数据库您可以显式设置spring.jpa.hibernate.ddl-auto,标准的Hibernate属性值为none、validate、update、create和create-drop。Spring Boot根据它认为的数据库是否嵌入式为您选择默认值。https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.data-initialization.using-hibernate “它认为” - granadaCoder
@eHayik 非常感谢。我为解决这个问题苦苦挣扎了两天。lol - navis1692

0
如果你正在开发一个maven项目,spring.jpa.defer-datasource-initialization=true应该在两个地方添加:src/main/resources/application.properties(让Spring Boot应用程序正常运行)和src/test/resources/application.properties (让mvn clean install执行成功)。
PS:我正在使用Spring Boot 3.0.2。

0

确保在此处添加h2数据库的依赖项,包括Gradle的依赖项

enter code here
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'

maven 依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=create-drop 转为 update,这意味着当重新运行应用程序或项目时,您的先前存储在数据库中的数据将不会丢失。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接