What is the right way for creating test data upon server startup and inserting them into the database (I'm using a JPA/JDBC backed Postgres instance).
Preferably in form of creating Entities and having them persisted through a Repository interface rather than writing plain SQL code. Something like RoR's Rake db:seed
helper.
If the framework exposes a hook for doing stuff when all the beans have been injected and the database is ready, that could also work.
You can catch
ApplicationReadyEvent
then insert demo data, for example:Or you can implement
CommandLineRunner
orApplicationRunner
, to load demo data when an application is fully started:Or even implement them like a Bean right in your Application (or other 'config') class:
From Spring documentation: http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-database-initialization
You can do like this