4

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?

Blay-Z
  • 81
  • 1
  • 2
  • 5

9 Answers9

5

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

manish
  • 151
  • 4
2

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")

anish sharma
  • 568
  • 3
  • 5
1

Check that you have added @Entity annotation to your model classes. Also, make sure that model classes are in the desired package.

S Badal
  • 71
  • 3
0

This is what you have to do:

1- Add @Entity annotation to every class you want to see as table

2- Add these lines in your application.properties:

spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=false 
spring.jpa.hibernate.ddl-auto=create-drop
0

Check these annotations: 1- @Table( name: ), @ Entity on you class. 2- @Column(name: ), on each field.

amir-rad
  • 85
  • 7
0

You can use this in the application.properties file it was work for me.

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE


spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.data.jpa.repositories.enabled=true
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driverClassName=org.postgresql.Driver

And also use this annotation above the entity class.

 @Entity
 @Table(name=" give the name of the table you want ")
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

My issue was that I created a new model, and one of the fields had a column name that was a reserved SQL keyword (e.g. "start", "end", "table", etc.) The table creation would fail due to a syntax error, but the application still started "successfully" for some reason.

Badr B
  • 998
  • 1
  • 9
  • 17
0

Make sure that your imports are from javax.persistence and not jakarta.persistence. Took me a while to solve this problem in a project where I was importing jakarta.persistence.Entity as well as other annotations. Once I switched all annotation imports to javax.persistence it all worked.

0

i faced this issue so in application.properties, i replace

spring.jpa.hibernate.ddl.auto=update

with

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

additionally i added(optional)

spring.jpa.defer-datasource-initialization=true
spring.jpa.database = MYSQL
Muhammad Ali
  • 1,055
  • 6
  • 11