Referencing Issue #621 - there is an issue where Boot has made the decision that all tables in an embedded database should be dropped on startup. I don't think Boot should default to doing that -- for one thing an embedded db is typically empty already unless you take measures to populate it, such as placing sql in schema.sql and data.sql. But even if it does, one should be able to override this behavior as described in the reference documentation, or the reference document should be updated to clarify that this default behavior cannot be overridden.
I'm using Boot 1.1.5 (current as of today). I'm populating an H2 DB with schema.sql and data.sql. After the SQL runs, JPA starts in drop-create mode and drops all of the tables. According to the current reference, One should be able to override this behavior with one of the following:
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.hbm2ddl.auto=none
But I've found these settings don't alter the behavior. I've also tried setting ddl-auto explicitly to "" without success. JPA happily deletes the tables and data that were previously created in the schema.sql and data.sql files.
The only workaround that I found was to use the hibernate-specific import.sql instead of schema.sql and data.sql files. When present, this keeps the drops from happening.
At minimum, perhaps the information on those settings can be clarified in the docs since the current reference makes it sound that these settings will in fact alter the behavior, see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-intialize-a-database-using-spring-jdbc
Comment From: dsyer
Setting spring.jpa.hibernate.ddl-auto=none should have worked, but maybe not if you set the other two as well. The logic for setting the default based on pre-existing settings is quite complicated, so perhaps there is a loop hole. Can you try that, and if it doesn't work put a breakpoint in JpaProperties.Hibernate.getAdditionalProperties() and see why it's not getting the right value (which should be null if you want Hibernate to not do anything)?
Comment From: kennyk65
Hi Dave - Thanks for the response. I have a sample project at https://github.com/kennyk65/spring-boot-jpa-table-drop-blues.git. I placed a breakpoint where you suggested, and also within the getOrDeduceDdlAuto() method. The matter seems to be decided by line 170 without any properties coming into play - though I don't really see where the 'existing' properties are supposed to come from.
I'm placing my parameters in application.properties
Comment From: bep
I had a similar issue (which turned out to be my fault - I had messed up the Yaml indentation), but I had a look at your test project.
Some issues there, mainly an empty application.properties - see kennyk65/spring-boot-jpa-table-drop-blues#1
Comment From: kennyk65
OK, so it looks like the root of the issue was my use of @ContextConfiguration rather than @SpringApplicationConfiguration in the integration test. I wasn't aware of the latter.
Using @SpringApplicationConfiguration allows the spring.jpa.hibernate.ddl-auto=none to work. Closing.
Comment From: lava18
I am still facing this problem and all documentation I read till now is not helping. Can anyone please help with what was the final solution for preventing JPA Hibernate to drop tables when server stops?
Comment From: snicoll
@lava18 we don't use the issue tracker for questions. Please ask on StackOverflow or join us on Gitter.
Comment From: SwingGuy1024
Thank you for the suggestion to add
spring.jpa.generate-ddl=false
to my application.properties file. Now I'm trying to figure out where in the Spring Boot documentation it says to use this.
Comment From: ssamal2801
spring.jpa.hibernate.ddl-auto=update
This will help; remember this property from hibernate .cfg file ?
Comment From: wilkinsona
@corneel We've changed our minds in the past and I am sure that we will again in the future, but this isn't a good way to ask us to do so. It leaves us frustrated as we struggle to assume positive intent and it leaves you frustrated as your suggestion isn't given serious consideration.
If you would like us to consider a change in behaviour here, please open a new issue that explains the problem that the current behaviour causes and what you think a better default would be.
Comment From: salueii
to disable table recreation in Spring Boot application just add this line
spring.jpa.generate-ddl=false
to application.properties