简体   繁体   English

Spring Boot 连接到 PostgreSQL 没有任何问题,但没有创建任何表

[英]Spring Boot connecting to PostgreSQL without any issue but is not creating any tables

I have a simple spring boot application with two entities, CustomUser and Role, the application connects to the PostgreSQL database without any issues but no tables are created.我有一个简单的 Spring Boot 应用程序,其中包含两个实体 CustomUser 和 Role,该应用程序连接到 PostgreSQL 数据库没有任何问题,但没有创建表。

application.yml:应用程序.yml:

server:
  port: 8082
spring:
  application:
    name: user-service
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/sdm-user-db
    username: root
    password: password
    # JPA properties
    jpa:
      hibernate:
        ddl-auto: create 
      show-sql: true
      database: postgresql
      database-platform: org.hibernate.dialect.PostgreSQLDialect
      open-in-view: false
      generate-ddl: true

  zipkin:
    base-url: http://localhost:9411/

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost

CustomUser:自定义用户:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
public class CustomUser implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
            name = "UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(36)")
    @Type(type = "uuid-char")
    private UUID id;
    private String name;
    @Column(unique=true)
    private String email;
    private String password;
    private String hashKey;
    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<Role> roles = new ArrayList<Role>();
}

Role:角色:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String name;
}

pom.xml database and jpa dependencies are included包含 pom.xml 数据库和 jpa 依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

The application runs without any issues so I have not much to debug with.该应用程序运行没有任何问题,所以我没有太多需要调试的地方。

pg admin showing empty tables for the database pg admin 显示数据库的空表

here I even try the connection with the database with the intelij plugin在这里我什至尝试使用 intelij 插件与数据库连接

successful connection test with intelij plugin使用 intelij 插件成功连接测试

then I tried a simple commandLineRunner where I save one role into the database, it fails and here is the caused by part of the error log然后我尝试了一个简单的 commandLineRunner 我将一个角色保存到数据库中,它失败了,这是由部分错误日志引起的

Caused by: org.postgresql.util.PSQLException: ERROR: relation "role" does not exist
  Position : 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:167) ~[postgresql-42.3.5.jar:42.3.5]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:135) ~[postgresql-42.3.5.jar:42.3.5]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-4.0.3.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
    ... 67 common frames omitted

如果您使用的是 Hibernate,将spring.jpa.hibernate.ddl-auto设置为update可能会有所帮助。

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

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