使用H2数据库自动创建的Spring Boot [英] Spring boot with H2 Database auto create

查看:105
本文介绍了使用H2数据库自动创建的Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Spring Boot在Java应用程序中使用嵌入式H2.当前,它在数据库中创建(覆盖)我的表.但是一旦我将其推向生产我不希望我的表被擦除并重新创建,因此我不会丢失表中的先前数据.但是我不确定如何实现这一目标.不知道在 spring.jpa.hibernate.ddl-auto = create 行中放什么.我尝试验证和其他选项,但没有解决.这是我的application.properties.谢谢

I am using embeded H2 in my java application via spring boot. Currently, it creates(overrides) my tables in the DB. But once I push it to production I do not want my tables to be wiped off and recreated, so I wont lose the previous datas in the tables. But I am not sure how to accomplish this. Not sure what to put on the line spring.jpa.hibernate.ddl-auto=create. I tried validate and other options but did not work out. Here is my application.properties. Thanks

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.initialization-mode=always
spring.jpa.hibernate.naming.physical- 
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

推荐答案

您可以显式设置spring.jpa.hibernate.ddl-auto,标准的Hibernate属性值无,验证,更新,创建和创建-drop

You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop

Spring Boot根据是否认为您的数据库已嵌入式为您选择默认值.如果未检测到模式管理器,或者在所有其他情况下均未检测到模式管理器,则默认为create-drop.通过查看连接类型可以检测到嵌入式数据库.hsqldb,h2和derby是嵌入式的,其他则不是.从内存数据库转换为真实"数据库时,请不要对新平台中表和数据的存在做出假设.您必须显式设置ddl-auto或使用其他机制之一来初始化数据库

Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no schema manager has been detected or none in all other cases. An embedded database is detected by looking at the Connection type. hsqldb, h2, and derby are embedded, and others are not. Be careful when switching from in-memory to a ‘real’ database that you do not make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database

以下情况可能对您适用:

Following cases might applicable for you:

更新:

例如,更新操作将尝试添加新的列,约束等,但绝不会删除以前可能存在但不再作为先前运行的对象模型一部分的列或约束.

The update operation for example will attempt to add new columns, constraints, etc but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run.

无:

在生产中,通常强烈建议您不使用或根本不指定此属性.这是因为DBA通常会检查迁移脚本以了解数据库更改,特别是如果您的数据库在多个服务和应用程序之间共享时.

In production, it's often highly recommended you use none or simply don't specify this property. That is because it's common practice for DBAs to review migration scripts for database changes, particularly if your database is shared across multiple services and applications.

此外,如果Hibernate从头开始创建模式(即,如果ddl-auto属性设置为create),则在启动时会在类路径的根目录中执行名为 import.sql 的文件.或create-drop).如果您小心的话,这对于演示和测试很有用,但是可能不希望出现在生产环境的类路径中.这是一个Hibernate功能(与Spring无关).

In addition, a file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop). This can be useful for demos and for testing if you are careful but is probably not something you want to be on the classpath in production. It is a Hibernate feature (and has nothing to do with Spring).

这篇关于使用H2数据库自动创建的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆