简体   繁体   English

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

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

I can't create a table in my PostgreSQL db through my Spring Boot Project.我无法通过 Spring Boot 项目在我的 PostgreSQL 数据库中创建表。

This is my table entity:这是我的表实体:

@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;
    }
}

In my application.properties there are:在我的 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

When I run the application I don't see any exceptions, but there is no table in the database.当我运行应用程序时,我没有看到任何异常,但数据库中没有表。
What can I do to solve the problem?我能做些什么来解决问题?

These are my logs:这些是我的日志:

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 is a keyword present in PostgreSQL, so you have to use another name for the user table. User 是 PostgreSQL 中存在的关键字,因此您必须为 user 表使用另一个名称。 Try this:-试试这个:-

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

Change table name from User to something else and provide default constructor in your entity class将表名从 User 更改为其他名称并在您的实体类中提供默认构造函数

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

User already exists in your PostgreSQL so it is not created so change it to "Users" or something else and also you are not creating a default constructor and hibernate required a default constructor.用户已存在于您的 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>

And remove并删除

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

Try this, setting up the version postgresql in your pom.xml .试试这个,在你的 pom.xml 中设置 postgresql 版本。 This is working for me.这对我有用。 Clean your project and update maven project.清理您的项目并更新 Maven 项目。

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

相关问题 在Spring Boot中在辅助数据库上创建表 - Create table on secondary db in spring boot 如何使用聚合类将现有的mongo数据库查询转换为Spring Boot - How can i convert my existing mongo db query in to spring boot using aggregation class 如何使用Spring Boot JPARepository更新postgres表? - How to update postgres table with Spring Boot JPARepository? 为什么当 spring 启动微服务启动时 Liquibase 不在 postgres 中创建我的表? - why Liquibase does not create my table in postgres when spring boot microservce is started? 启动Spring Boot后,如何在数据库中“还原”数据? - How can I “restore” data in DB after start Spring boot? 如何在Spring Boot中模拟数据库连接以进行测试? - How can I mock db connection in Spring Boot for testing purpose? 我无法创建我的自定义 spring-boot 启动器 - I can't create my custom spring-boot starter 使用 Spring 引导,如何在新表上创建 2 个实体之间的关系并为其提供额外的列? - Using Spring Boot, how can I create the relationship between 2 entities on a new table and give it extra columns? 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot 删除数据库中的所有表后,如何在数据库中创建表? - How can I create a table in my DB, after dropping all of my tables in database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM