【问题标题】:How to generate Postgres tables correctly from spring boot application?如何从 Spring Boot 应用程序正确生成 Postgres 表?
【发布时间】:2019-07-17 10:21:48
【问题描述】:

我正在使用 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() {

    }





}

application.propreties:

# ===============================
# 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> {
}

【问题讨论】:

    标签: postgresql spring-boot


    【解决方案1】:

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

    否则实体将不会生成。

    【讨论】:

    • 感谢您的回答!我遇到了同样的问题,然后我将我的实体移动到包含 DemoApplication 类的包的子包中,它终于工作了!
    【解决方案2】:

    尝试将spring.jpa.hibernate.ddl-auto 属性更改为create 值。

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2019-06-17
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2022-10-21
      • 2018-12-30
      • 2020-03-30
      • 2021-05-30
      相关资源
      最近更新 更多