0

I setup 3 profiles ( dev/test/prod ) workin like magic ( new to java and spring boot )

In my dev profile, database is generated with

hibernate.ddl-auto

workin great... also with

.hibernate.hbm2ddl.import_files

witch I use to populate the db with test-data

in the test-environment, I'd like to run a script instead, which sets up the db from a dump.. to be more like production (entity does not match db for some reason)

so I tried to use:

datasource.data 

&& ||

datasource.schema

in different combinations, with file: , classpath prefixed .....

when I do:

datasource.data=data.sql

it's saying:

'ServletContext resource [/data.sql]' is invalid: The specified resource does not exist

... moved the file around .... /src/main /src/main/resources /src/main/resources/META-INF .. all that stuff

I don't get what it means by "ServletContext" ........

I did disable

hibernate.ddl-auto

by setting it to none in that profile

also

hibernate.jpa.generate-ddl

to false, even it is not needed / used when setting ddl-auto

...on my quest "google-searching" this, I came across posts from 2014 and got totally confused since things emerged from then ......

there is, I count on that ;) , some property which must be set to "runThisScript" but I got through the docs ...... possibly I missed something

someone can point me in the right direction?
thank you!

thx @Blagoj for your fast response, the link helped me .. the script gets executed by setting ddl-auto to create .. but it generates from entity too ... how to stop it from doing that && have the script executed ?

thx @luboskrnac for your advice! will take a look / use it ! .. what bothers me is, that i have to do it wrong/not so good from my oppinion... in dev to match prod ..... had hoped to do it right in dev and have prod on that state once i could proove that it is right ;)

... ok for now i ended up setting up test-environment by hand .. but will try to automate it once i know more ... i see there is always a tool for the injected-problem ;)

womd
  • 3,077
  • 26
  • 20
  • https://stackoverflow.com/questions/673802/how-to-import-initial-data-to-database-with-hibernate – Blagoj Atanasovski Oct 12 '18 at 06:47
  • thank you for your fast response ... i set generate-ddl to "create" , which from my understanding would generate the db from entity && execute script , but seems workin ....have to take closer look what it did .... got an error in sql-script now ;) – womd Oct 12 '18 at 06:57

1 Answers1

1

Use DB migration tool like Flyway or Liquibase to create initial schema and for incremental migrations of DB schema. Now important point: Do it the same way in DEV and PROD env! Otherwise you are asking for troubles.

Initial schema can be generated by help of Hibernate. spring.jpa.hibernate.ddl-auto=create will create schema from your JPA model + logging.level.org.hibernate.SQL=DEBUG will bump SQL commands to create this schema. This SQL dump can be than used as initial migration script for Flyway or Liquibase.

You can even insert sample data into PROD (if you can hide them behind testing account). Than you can run automated testing against PROD instance, which can be handy to verify that important features are working in PROD.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • my advice would be "if you are only bound to one dbms, then only rely on liquibase's scripts, do not rely on hbm2ddl", because it allows very fine grained sql type and constraints. Mixing hbm2ddl and liquibase could lead to unexpected results when switching from one dbms to another. – spi Oct 12 '18 at 07:54
  • I mentioned to use db migration tool exclusively + generate **initial** schema with `ddl-auto`. My approach is not mixing them. – luboskrnac Oct 12 '18 at 11:33