Spring Boot 应用程序 Heroku PostgreSQL 错误:GenerationTarget 遇到异常接受命令:执行 DDL 时出错 ...通过 JDBC 语句

Posted

技术标签:

【中文标题】Spring Boot 应用程序 Heroku PostgreSQL 错误:GenerationTarget 遇到异常接受命令:执行 DDL 时出错 ...通过 JDBC 语句【英文标题】:Spring Boot app Heroku PostgreSQL error : GenerationTarget encountered exception accepting command : Error executing DDL ... via JDBC Statement 【发布时间】:2021-03-18 01:40:12 【问题描述】:

我使用 Spring Boot 创建了一个小型社交媒体应用程序,在开发过程中我使用了 H2 本地数据库,它运行良好。现在我尝试将其连接到 Heroku PostgreSQL 数据库,但标题中出现错误。任何想法为什么? 我怀疑是一种错误的方言,但在我检查的所有地方,我都应该像现在一样使用 org.hibernate.dialect.PostgreSQLDialect。否则,实体或映射可能会出现一些意外?即使是,我也无法弄清楚要修改什么以及如何修改它们。 我对数据库的东西很陌生,所以如果这是一个愚蠢的错误,请不要评判我。

实体类:用户

package com.schabby.socialplatform.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class User implements Serializable 
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY, generator="seq")
    private Long id;
    
    private String username;
    private String password;
    private String name;
    private int age;
    private String picture;
    private boolean online;
    
    @JsonIgnore
    @OneToMany(mappedBy = "User")
    private Collection<Post> posts = new ArrayList<Post>();

    public User() 
    

    public User(String username, String password, String name, int age, String picture, boolean online) 
        this.username = username;
        this.password = password;
        this.name = name;
        this.age = age;
        this.picture = picture;
        this.online = online;
    
    
    public Long getId() 
        return id;
    

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

    public String getUsername() 
        return username;
    

    public void setUsername(String username) 
        this.username = username;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public int getAge() 
        return age;
    

    public void setAge(int age) 
        this.age = age;
    

    public Collection<Post> getPosts() 
        return posts;
    

    public void setPosts(Collection<Post> posts) 
        this.posts = posts;
    

    public boolean isOnline() 
        return online;
    

    public void setOnline(boolean online) 
        this.online = online;
    

    public String getPicture() 
        return picture;
    

    public void setPicture(String picture) 
        this.picture = picture;
    
    
    
    
 

实体类:帖子

package com.schabby.socialplatform.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;

@Entity
public class Post implements Serializable 
    
    @Id
    @GeneratedValue
    private long id;
    
    private String text;
    
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;
    
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="User_id")
    private User User;
    
    
    public Post() 
    

    public Post(String text, Date date, User user) 
        this.text = text;
        this.date = date;
        this.User = user;
    

    public long getId() 
        return id;
    

    public void setId(long id) 
        this.id = id;
    

    public String getText() 
        return text;
    

    public void setText(String text) 
        this.text = text;
    

    public Date getDate() 
        return date;
    

    public void setDate(Date date) 
        this.date = date;
    

    public User getUser() 
        return User;
    

    public void setUser(User user) 
        this.User = user;
    

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.schabby</groupId>
    <artifactId>socialplatform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>socialplatform</name>
    <description>Social Media Platform</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
                <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.1</version>
        </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <scope>runtime</scope>
                </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
     <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
      <type>jar</type>
     </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

spring.datasource.url=jdbc:postgresql://ec2-54-217-206-236.eu-west-1.compute.amazonaws.com:5432/d5rk5sir0732uc?sslmode=require
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create

感谢您的每一个建议!

【问题讨论】:

【参考方案1】:

我在 *** 上找到了答案!

问题是某些名称,例如我的表名:'user' 是为 PostgreSQL 保留的。因此,更改它的名称解决了我的问题。

这是原始答案: https://***.com/a/56206827/13304501

【讨论】:

好吧,我花了几个小时来处理这个问题。非常感谢你发布这个。这完全解决了我的问题。如果你最常使用 user 作为表名,你可以用 @Table(name="\"user\"") 标记你的实体,这样就可以了。

以上是关于Spring Boot 应用程序 Heroku PostgreSQL 错误:GenerationTarget 遇到异常接受命令:执行 DDL 时出错 ...通过 JDBC 语句的主要内容,如果未能解决你的问题,请参考以下文章

使用 nginx 作为代理的 Spring Boot 应用程序部署在 Heroku 上

在heroku上部署spring boot应用程序的问题

Spring Boot应用程序在Heroku代码= H10上崩溃

在 Heroku 中部署我的 Gradle (Spring Boot) 应用程序

为 Java Spring Boot Gradle 项目构建 Heroku Procfile

Heroku 无法部署 Java 11 Spring Boot App [重复]