2

I just started to Spring Boot and managed to create table in PostgreSQL database. Then I add a config file to my project that would insert data to database. However, as far as I see, the code does not hit this config file and re-create table. I tried to change spring.jpa.hibernate.ddl-auto parameter in application.properties file, but it does not make any sense and as a result, the table is re-created on each app running with empty record (it is also seen on Java console just create table). SO, what I am missing?

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/student
spring.datasource.username=postgres
spring.datasource.password=******
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

StudentConfig:

@Configuration
public class StudentConfig {

    CommandLineRunner commandLineRunner(StudentRepository repository) {
        return args -> {
            Student johnson = new Student(
                    "johnson@gmail.com",
                    "Johnson",
                    LocalDate.of(2000, JANUARY, 5)
            );

            Student alex = new Student(
                    "alex@gmail.com",
                    "alex",
                    LocalDate.of(2010, JANUARY, 17)
            );

            repository.saveAll(List.of(johnson, alex));
        };
    }
}
  • Does anybody else use Spring Boot and Data JPA? –  May 15 '21 at 10:32
  • What exactly "does not make any sense"? When you set it to `create-drop`, the schema will indeed be created again when you run your application. Set it to something else, for example `validate` or `update`. See [How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?](https://stackoverflow.com/questions/42135114/how-does-spring-jpa-hibernate-ddl-auto-property-exactly-work-in-spring) – Jesper May 15 '21 at 10:41
  • 1
    Probably I am wrong, have you tried to use `@Bean` over the `commandLineRunner` ? – dariosicily May 15 '21 at 16:45
  • 1
    @dariosicily Yessss, exactly!!! Thanks a lot, after adding `@Bean` over the `commandLineRunner`, **the problem fixed**. Thanks a lot :) Voted up ;) –  May 15 '21 at 20:37
  • 1
    You are welcome :) , you can check [accessing-data-jpa](https://spring.io/guides/gs/accessing-data-jpa/) where at the end is written *The demo() method returns a CommandLineRunner bean that automatically runs the code when the application launches.* , and the same thing happens because it is inside your `StudentConfig` class annotated with `@Configuration`. – dariosicily May 15 '21 at 20:45
  • Seems to be very useful, I have a look at. –  May 15 '21 at 21:03

2 Answers2

1

spring.jpa.hibernate.ddl-auto should not be "create-drop", if you want your table to not be re-created. Try with "update" or "none" and make sure that your application uses that application.properties file

B. Bal
  • 121
  • 1
  • 2
  • 11
  • I tried both of them (`spring.jpa.hibernate.ddl-auto=none` and ``spring.jpa.hibernate.ddl-auto=update`) before askingt the question, but even if the table is not recreated, the config code is not initialized and records are not created. –  May 15 '21 at 12:59
1

Try and change: spring.jpa.hibernate.ddl-auto=create-drop

to spring.jpa.hibernate.ddl-auto=update

in the application.properties

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
Makhanya
  • 11
  • 3
  • I tried both of them (`spring.jpa.hibernate.ddl-auto=none` and ``spring.jpa.hibernate.ddl-auto=update`) before askingt the question, but even if the table is not recreated, the config code is not initialized and records are not created. –  May 15 '21 at 12:59