Reputation:
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(
"[email protected]",
"Johnson",
LocalDate.of(2000, JANUARY, 5)
);
Student alex = new Student(
"[email protected]",
"alex",
LocalDate.of(2010, JANUARY, 17)
);
repository.saveAll(List.of(johnson, alex));
};
}
}
Upvotes: 2
Views: 1235
Reputation: 11
Try and change: spring.jpa.hibernate.ddl-auto=create-drop
to spring.jpa.hibernate.ddl-auto=update
in the application.properties
Upvotes: 1
Reputation: 121
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
Upvotes: 1