Blay-Z
Blay-Z

Reputation: 81

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?

Upvotes: 4

Views: 15802

Answers (12)

DiaMaBo Dev
DiaMaBo Dev

Reputation: 1

I encounter the same error creation of the table and after having a long day, I mistakenly add String as type instead of Long

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private **String** id; <-- Long id;

Upvotes: 0

Muhammad Ali
Muhammad Ali

Reputation: 1151

I faced this issue, so in application.properties, I replaced

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

Upvotes: 1

anish sharma
anish sharma

Reputation: 590

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

Upvotes: 2

manish
manish

Reputation: 161

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.

Upvotes: 5

KARTHICK R
KARTHICK R

Reputation: 1

In my case i have @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) which is the RCA and fix is to remove the exclude

Upvotes: -1

g_mdr
g_mdr

Reputation: 1

I was also facing same issue. because I was using spring.jpa.properties.hibernate.dialect as org.hibernate.dialect.MySQLDialect replaced by org.hibernate.dialect.MySQL5InnoDBDialect and table got created

Upvotes: 0

Brandon Hauser
Brandon Hauser

Reputation: 1

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.

Upvotes: 0

Badr B
Badr B

Reputation: 1435

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.

Upvotes: 0

S Badal
S Badal

Reputation: 71

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

Upvotes: 1

Kaushik Shaw
Kaushik Shaw

Reputation: 41

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

Upvotes: 0

amir rad
amir rad

Reputation: 122

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

Upvotes: 0

Descartes fowo
Descartes fowo

Reputation: 1

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

Upvotes: 0

Related Questions