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.

0

2 Answers 2

1

I would suggest to use Flyway or Liquibase

Both have good tuning with Spring.

Sign up to request clarification or add additional context in comments.

3 Comments

I prefer Liquibase e.g. using a CSV file together with a changeSet the is defined with runOnChange="true"
Does flyway insert the data every time application starts, or its a one time activity? @Yogesh Prajapati
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.