简体   繁体   中英

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.

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:

### 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. 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

@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.

  @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 . This is working for me. Clean your project and update maven project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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