[英]Is it possible to make Spring Boot JPA Hibernate application to automatically create tables with necessary columns and relations from JPA entities?
[英]Spring Boot - JPA Hibernate not creating tables automatically?
我的application.properties
文件中有此代碼:
# 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
如您所見,我添加了spring.jpa.hibernate.ddl-auto=create
行,但 JPA 仍未創建表。 我必須手動創建它們才能編譯項目。 怎么了?
您可以使用spring.jpa.hibernate.ddl-auto=update
並檢查您是否在實體類上使用 @Table(name="table_name") 頂部。 它可能有幫助
您可以在 application.properties 文件中使用它,它對我有用。
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
並在實體類上方使用此注釋。
@Entity
@Table(name=" give the name of the table you want ")
檢查您是否已將 @Entity 注釋添加到模型類中。 此外,請確保模型類位於所需的包中。
有幾種可能的原因:
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.
嘗試添加@ComponentScan("包含實體類、配置和服務的包")
這是你必須做的:
1- 將@Entity
注釋添加到您希望作為表格查看的每個類
2- 在 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
檢查這些注釋: 1- @Table( name: ), @ Entity 在你的課上。 2- @Column(name: ),在每個字段上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.