spring-boot 没有创建 hsqldb 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26663308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:10:13  来源:igfitidea点击:

spring-boot is not creating hsqldb database

springhibernatespring-mvcspring-bootspring-data-jpa

提问by Kleber Mota

In my current spring project, I start to use spring-boot with spring-jpa to create and manage a HSQLDb database.

在我目前的spring项目中,我开始使用spring-boot和spring-jpa来创建和管理一个HSQLDb数据库。

I have this hibernate.propertiesin thee folder src/main/resourcesfrom my project:

hibernate.propertiessrc/main/resources我的项目的文件夹中有这个:

# jdbc.X
jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:file:/home/kleber/.webapp/testedb
jdbc.user=sa
jdbc.pass=

# hibernate.X
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create

my pom.xml have this dependencies:

我的 pom.xml 有这个依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

my main class is that:

我的主要课程是:

@Controller
@EnableJpaRepositories
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @RequestMapping(value = "/signin")
    public String signin(Model model) {
        return "acesso/signin";
    }

    @RequestMapping(value = "/admin")
    public String admin(Model model) {
        return "private/admin";
    }

    @RequestMapping(value = "/")
    public String index(Model model) {
        return "public/index";
    }

}

when I run the application, I can see this in the console:

当我运行应用程序时,我可以在控制台中看到:

2014-10-30 17:58:51.708  INFO 31413 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.6.Final}
2014-10-30 17:58:51.713  INFO 31413 --- [           main] org.hibernate.cfg.Environment            : HHH000205: Loaded properties from resource hibernate.properties: {jdbc.url=jdbc:hsqldb:file:/home/kleber/.webapp/testedb, hibernate.dialect=org.hibernate.dialect.HSQLDialect, hibernate.show_sql=false, jdbc.user=sa, hibernate.bytecode.use_reflection_optimizer=false, hibernate.hbm2ddl.auto=create, jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver, jdbc.pass=}
2014-10-30 17:58:51.714  INFO 31413 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2014-10-30 17:58:52.089  INFO 31413 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2014-10-30 17:58:52.191  INFO 31413 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
2014-10-30 17:58:52.385  INFO 31413 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2014-10-30 17:58:52.838  INFO 31413 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2014-10-30 17:58:52.845  INFO 31413 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

but no database is created in the path defined by the property jdbc.url.

但没有在属性定义的路径中创建数据库jdbc.url

ANyone can tell me what I am doing wrong?

任何人都可以告诉我我做错了什么?

回答by Andy Wilkinson

You'd be better using Spring Boot to manage your configuration. It will pass Hibernate configuration to Hibernate while also auto-creating your DataSource and database for you.

您最好使用 Spring Boot 来管理您的配置。它会将 Hibernate 配置传递给 Hibernate,同时还会为您自动创建数据源和数据库。

I would move all of your configuration into src/main/resources/application.properties:

我会将您的所有配置移至src/main/resources/application.properties

# DataSource
spring.datasource.url=jdbc:hsqldb:file:/home/kleber/.webapp/testedb
spring.datasource.username=sa

# Hibernate
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

I've removed some unnecessary configuration. For example, the configuration of driverClassNameas Spring Boot will infer it from the url and the empty DataSource password as that's the default. You can see some documentation of the configuration properties and their default values here.

我删除了一些不必要的配置。例如,driverClassNameas Spring Boot的配置将根据 url 和空的 DataSource 密码推断它,因为这是默认值。您可以在此处查看配置属性及其默认值的一些文档。