Spring boot with H2 Database auto create

余生颓废 提交于 2021-02-10 12:42:35

问题


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

回答1:


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 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:

update:

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.

none:

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.

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).




回答2:


You should use update here:

spring.jpa.hibernate.ddl-auto=update

This will build the tables on startup if they're not already defined, otherwise it will leave them as they are.

Also when you do that, you might want to drop the line:

spring.jpa.generate-ddl=true

Finally, the best way to do this is to have different sets of properties for different environments, either by passing them in as run time variables or ideally externalising them with something like spring config server.




回答3:


I was missing below line.

spring.jpa.hibernate.ddl-auto=update



来源:https://stackoverflow.com/questions/51691744/spring-boot-with-h2-database-auto-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!