Hibernate ddl-auto = update无效

时间:2016-09-24 04:17:06

标签: spring hibernate

每次重新启动tomcat服务器时,都会删除数据库表中的所有数据。但我想将数据保存在数据库表中。在阅读了几篇帖子后我到目前为止所理解的是ddl-auto应该设置为更新。

在我的春季启动应用程序中,我有以下设置

security.basic.enabled=false
server.port=9090
spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
hibernate.show_sql = true
hibernate.format_sql = true
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

不幸的是,这没有按预期工作。表中的所有数据都将被删除。

这是日志

2016-09-24 09:35:47.831  INFO 6343 --- [ost-startStop-1] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-09-24 09:35:47.852  INFO 6343 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2016-09-24 09:35:47.975  INFO 6343 --- [ost-startStop-1] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.11.Final}
2016-09-24 09:35:47.979  INFO 6343 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-09-24 09:35:47.982  INFO 6343 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-09-24 09:35:48.320  INFO 6343 --- [ost-startStop-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2016-09-24 09:35:48.954  INFO 6343 --- [ost-startStop-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2016-09-24 09:35:49.211  INFO 6343 --- [ost-startStop-1] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-09-24 09:35:49.861  INFO 6343 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2016-09-24 09:35:49.861  INFO 6343 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
2016-09-24 09:35:49.866  INFO 6343 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
2016-09-24 09:35:49.882  INFO 6343 --- [ost-startStop-1] java.sql.DatabaseMetaData                : HHH000262: Table not found: user
2016-09-24 09:35:49.885  INFO 6343 --- [ost-startStop-1] java.sql.DatabaseMetaData                : HHH000262: Table not found: user
2016-09-24 09:35:49.887  INFO 6343 --- [ost-startStop-1] java.sql.DatabaseMetaData                : HHH000262: Table not found: user
2016-09-24 09:35:49.907  INFO 6343 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete

我真正需要的是将数据保留在数据库表中,即使在重新启动我的服务器之后也是如此。

2 个答案:

答案 0 :(得分:2)

它应该只通过指定这两个属性来工作:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:file:./testdb

使用spring-boot 1.4.1测试。

答案 1 :(得分:0)

如果仍然无法正常运行,请尝试以下操作(Spring Boot 2.1)
   替换

String startOfMonth = "20191201";
String endOfMonth = "20191231";

使用

spring.jpa.hibernate.ddl-auto=update