Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - JPA Hibernate not creating tables automatically?

I have this code in my application.properties file:

# Spring DataSource
spring.datasource.driverClassName=org.postgresql.Driver
spring.sql.init.mode=always
spring.sql.init.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=root

# JPA-Hibernate
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

# https://stackoverflow.com/questions/43905119/postgres-error-method-org-postgresql-jdbc-pgconnection-createclob-is-not-imple
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

# Optimization for POSTGRES queries
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect

As you can see I have the spring.jpa.hibernate.ddl-auto=create line added but the tables are still not being created by the JPA. I have to manually create them in order for the project to compile. What is wrong?

like image 560
Blay-Z Avatar asked Nov 01 '25 21:11

Blay-Z


2 Answers

You can use spring.jpa.hibernate.ddl-auto=update and check you are using @Table(name="table_name") top on the entity class. It may help.

like image 123
manish Avatar answered Nov 03 '25 11:11

manish


There are several possible causes:

  • Your entity classes are in the same or in a sub-package relative one where you have you class with @EnableAutoConfiguration. If not then your spring app does not see them and hence will not create anything in db
  • Your application.properties must be in src/main/resources folder.

Try adding @ComponentScan("package which contains entity classes, configurations and services")

like image 36
anish sharma Avatar answered Nov 03 '25 13:11

anish sharma