【问题标题】:Spring Boot - JPA Hibernate not creating tables automatically?Spring Boot - JPA Hibernate 不会自动创建表?
【发布时间】:2021-09-15 05:33:57
【问题描述】:

我的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 仍未创建表。我必须手动创建它们才能编译项目。怎么了?

【问题讨论】:

    标签: java postgresql spring-boot hibernate jpa


    【解决方案1】:

    你可以使用spring.jpa.hibernate.ddl-auto=update 并检查你是否在实体类上使用@Table(name="table_name") top。 它可能会有所帮助

    【讨论】:

      【解决方案2】:

      有几种可能的原因:

      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("包含实体类、配置和服务的包")

      【讨论】:

        【解决方案3】:

        这是你必须做的:

        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
        

        【讨论】:

          【解决方案4】:

          检查这些注释: 1- @Table( name: ), @Entity 在你的班级。 2- @Column(name: ),在每个字段上。

          【讨论】:

            【解决方案5】:

            您可以在对我有用的 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 ")
            

            【讨论】:

              【解决方案6】:

              检查您是否已将@Entity 注解添加到您的模型类中。此外,请确保模型类在所需的包中。

              【讨论】:

                猜你喜欢
                • 2019-06-12
                • 2021-10-21
                • 2021-08-13
                • 2016-05-26
                • 2017-08-11
                • 1970-01-01
                • 2016-10-01
                • 2016-10-29
                • 2018-11-19
                相关资源
                最近更新 更多