首页 > 解决方案 > 如何从 Spring Boot 应用程序正确生成 Postgres 表?

问题描述

我正在使用 spring-boot 为未来的 Angular 前端开发 RESTFUL API。我在将我的实体创建到 postgres 表中时遇到了这个问题。

检查与数据库的连接性,一切正常。使用 mvn clean install & mvn spring-boot run 命令生成正常的 tomcat 部署,没有任何错误。但是没有创建表

这是我的代码:实体:

@Entity
@Table(name = "questions")
public class Question {
    @Id
    @GeneratedValue(generator = "question_generator")
    @SequenceGenerator(
            name = "question_generator",
            sequenceName = "question_sequence",
            initialValue = 1000
    )
    private Long id;

    @NotBlank
    @Size(min = 3, max = 100)
    private String title;

    @Column(columnDefinition = "text")
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Question() {

    }





}

应用程序属性:

# ===============================
# DATABASE CONNECTION
# ===============================

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

# ===============================
# JPA / HIBERNATE
# ===============================

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect


# Fix Postgres JPA Error:
# Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

这是我的回购:

import model.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
}

标签: postgresqlspring-boot

解决方案


我能够通过更改实体包并使其对应用程序可见来解决此问题。现在它工作得很好。主包名是:com.testAppBlaBla 实体的包应该是 com.testAppBlaBla.model

否则不会生成实体。


推荐阅读