0

I am Working on a spring boot project using PostgreSQL DB. I want to insert some static data in the DB, which will never be changed. What is the best way to perform this:-

1- Using .sql file in my classpath. But the problem is, it will always insert the data whenever the application starts.

2- Using insert query in the DB. Only one-time operation.

3- Using dataInitializer in spring boot main class and inserting the data from the code.

Thanks in advance.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
Danish
  • 189
  • 2
  • 3
  • 14

2 Answers2

1

I would suggest to use Flyway or Liquibase

Both have good tuning with Spring.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
  • I prefer Liquibase e.g. using a CSV file together with a changeSet the is defined with `runOnChange="true"` –  Sep 24 '19 at 10:46
  • Does flyway insert the data every time application starts, or its a one time activity? @Yogesh Prajapati – Danish Sep 24 '19 at 10:57
  • It's one-time activity, you have to create a file under resource folder with version. you can create new file if you need to do some more operation in new release. – Yogesh Prajapati Sep 24 '19 at 11:02
0

Hope you are using hibernate as ORM in your application for db configuration.

Using hibernate you can control when your sql file should be used and insert statement should get executed,

One property is available for hibernate configuration named, hibernate.hbm2ddl.import_files = path to sql file

Above property will be used to provide sql file path and file will get executed only when hibernate.hbm2ddl.auto = create or create-drop modeis provided.

Now second time onwards when ever your application starts just modify property hibernate.hbm2ddl.auto = validate.

Sql file import will never be executed with above mentioned mode.