在我目前的春季项目中,我开始使用spring-boot和spring-jpa来创建和管理HSQLDb数据库。
我的项目hibernate.properties
中有src/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
:
<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>
我的pom.xml有这种依赖关系:
@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";
}
}
我的主要课程是:
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
当我运行应用程序时,我可以在控制台中看到它:
jdbc.url
但在属性{{1}}定义的路径中没有创建数据库。
任何人都可以告诉我我做错了什么?
答案 0 :(得分:7)
您最好使用Spring Boot来管理配置。它会将Hibernate配置传递给Hibernate,同时还为您自动创建数据源和数据库。
我会将您的所有配置移到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
我删除了一些不必要的配置。例如,driverClassName
作为Spring Boot的配置将从url和空DataSource密码推断它,因为它是默认值。您可以查看配置属性的一些文档及其默认值here。