简体   繁体   中英

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?

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM