繁体   English   中英

如何使用 Spring Boot 在我的 postgres 数据库中创建一个表?

[英]How can I create a table in my postgres db with Spring Boot?

我无法通过 Spring Boot 项目在我的 PostgreSQL 数据库中创建表。

这是我的表实体:

@Entity
@Table(name = "User")
public class User {

    @Column(name = "name")
    private String name;

    @Column(name = "surname")
    private String surname;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

在我的 application.properties 中有:

### POSTGRES ###
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/vpfs
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name: org.postgresql.Driver

spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.continue-on-error=true

当我运行应用程序时,我没有看到任何异常,但数据库中没有表。
我能做些什么来解决问题?

这些是我的日志:

2021-07-16 12:45:34.461  INFO 27428 --- [           main] com..iot.simplemicroservice.App   : The following profiles are active: dev
2021-07-16 12:45:35.917  INFO 27428 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-07-16 12:45:35.959  INFO 27428 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 34ms. Found 0 JPA repository interfaces.
2021-07-16 12:45:36.237  INFO 27428 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=522f302b-c497-3a88-810d-f5887a27afd9
2021-07-16 12:45:36.309  INFO 27428 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'persistenceJPAConfig' of type [com..iot.simplemicroservice.util.PersistenceJPAConfig$$EnhancerBySpringCGLIB$$15e8ab6c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-07-16 12:45:36.465  INFO 27428 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@5fb07347' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-07-16 12:45:36.476  INFO 27428 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-07-16 12:45:36.498  INFO 27428 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-07-16 12:45:36.738  INFO 27428 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8087 (http) 10022 (http) 10080 (http) 10081 (http) 10443 (http) 13333 (http) 13334 (http) 13335 (http) 16010 (http) 18484 (http) 18485 (http) 18660 (http) 18663 (http) 18081 (http) 18887 (http) 18888 (http) 18889 (http) 18890 (http) 40004 (http) 40005 (http) 40007 (http) 40008 (http) 40009 (http) 40010 (http) 40011 (http) 40012 (http) 40013 (http) 40014 (http) 40015 (http) 40030 (http) 40040 (http) 40202 (http) 40206 (http) 40303 (http)
2021-07-16 12:45:37.566  INFO 27428 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-07-16 12:45:37.566  INFO 27428 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2021-07-16 12:45:37.758  INFO 27428 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-07-16 12:45:37.758  INFO 27428 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3276 ms
2021-07-16 12:45:38.083  INFO 27428 --- [           main] c.n.i.s.c.security.FourStoreXssFilter    : Filter Initialization
2021-07-16 12:45:39.047  INFO 27428 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-07-16 12:45:40.033  INFO 27428 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-07-16 12:45:40.213  INFO 27428 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

2021-07-16 12:45:40.570  WARN 27428 --- [           main] o.s.c.n.a.ArchaiusAutoConfiguration      : No spring.application.name found, defaulting to 'application'
2021-07-16 12:45:40.711  WARN 27428 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

User 是 PostgreSQL 中存在的关键字,因此您必须为 user 表使用另一个名称。 试试这个:-

  @Entity
  @Table(name = "Users")
  public class User {
  ..........
  }

将表名从 User 更改为其他名称并在您的实体类中提供默认构造函数

@Entity
@Table(name = "table_name") // name other than User
public class User {
  User(){
}
}

用户已存在于您的 PostgreSQL 中,因此未创建它,因此将其更改为“用户”或其他内容,并且您没有创建默认构造函数,并且休眠需要默认构造函数。

  @Entity
  @Table(name = "Users")
  public class User {

    public User(){

    }

  }

使用 spring.jpa.hibernate.ddl-auto=create 或 create-drop 创建后可以改回更新

    <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>${postgrejar.version}</version>
            </dependency>

   <properties>
        <postgrejar.version>42.2.18</postgrejar.version>
    </properties>

并删除

spring.datasource.driver-class-name: org.postgresql.Driver

试试这个,在你的 pom.xml 中设置 postgresql 版本。 这对我有用。 清理您的项目并更新 Maven 项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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