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?

1
  • maybe you use some keywords which reserved by jpa in you entities as fields. Commented Jan 6, 2023 at 15:08

12 Answers 12

5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Comments

1

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

Comments

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

Comments

0

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

Comments

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

Comments

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.

Comments

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.

1 Comment

jakarta is a rebrand of the javax package (because of Oracle). Changing the package to javax will be relevant when working with Spring Boot 2, as for Spring Boot 3 - you'll need to use jakarta
0

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

Comments

0

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;

Comments

-1

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

1 Comment

I think you proposed an answer where you meant to add a comment to an existing answer. If this is not the case, then your answer needs to have a little more detail about why you feel that what you have will solve the asker's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.