从spring创建postgres数据库时不存在关系

时间:2018-11-21 20:45:33

标签: spring postgresql

我想在spring应用程序启动时自动创建postgres数据库架构。
我正在从pgadmin手动创建一个空数据库,当我第一次运行spring应用程序时,我得到了以下错误日志: full log

主要的主要错误是:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table day_places drop constraint FKscp1fnpwfhy14rf7tiur81akl" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: relation "day_places" does not exist
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table forecast drop constraint FKetkl57pr3fjhsv16f54gbbtbw" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: relation "forecast" does not exist
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table forecast drop constraint FKqo2b7v7gs8cgevlnvi1n7evxs" via JDBC Statement

Caused by: org.postgresql.util.PSQLException: ERROR: relation "forecast" does not exist

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table forecasts_forecast drop constraint FK160711nhmaixbibipj9v6j57t" via JDBC Statement

Caused by: org.postgresql.util.PSQLException: ERROR: relation "forecasts_forecast" does not exist

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table forecasts_forecast drop constraint FKeuqqos8m3g9yffrahb4hy5up6" via JDBC Statement

Caused by: org.postgresql.util.PSQLException: ERROR: relation "forecasts_forecast" does not exist

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table nights_places drop constraint FKaptmwcetlole3dc41suixjhts" via JDBC Statement

Caused by: org.postgresql.util.PSQLException: ERROR: relation "nights_places" does not exist

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table nights_places drop constraint FKgpbig7892x68kjxtf4lfbid08" via JDBC Statement

Caused by: org.postgresql.util.PSQLException: ERROR: relation "nights_places" does not exist

正如我注意到的那样,错误仅与我标注为@onetomany的类的字段有关。
即使显示这些错误,仍然可以按预期创建和使用表。
第一次运行后,这些错误不再显示,但是我仍然想解决这些问题。那么如何在启动时创建这些关系呢?任何帮助将不胜感激

package home.persistence.domain.model;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@Entity
@XmlRootElement(name="day")
@Table(name = "day")
public class Day
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    private String phenomenon;
    private String tempmax;
    private String tempmin;
    private String text;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Place> places;

    public List<Place> getPlaces() {
        return places;
    }
    @XmlElement(name = "place")
    public void setPlaces(List<Place> places) {
        this.places = places;
    }

    public String getPhenomenon ()
    {
        return phenomenon;
    }

    public void setPhenomenon (String phenomenon)
    {
        this.phenomenon = phenomenon;
    }

    public String getTempmax ()
    {
        return tempmax;
    }

    public void setTempmax (String tempmax)
    {
        this.tempmax = tempmax;
    }

    public String getTempmin ()
    {
        return tempmin;
    }

    public void setTempmin (String tempmin)
    {
        this.tempmin = tempmin;
    }

    public String getText ()
    {
        return text;
    }

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

    @Override
    public String toString()
    {
        return "ClassPojo [phenomenon = "+phenomenon+", tempmax = 
    "+tempmax+", tempmin = "+tempmin+", text = "+text+"]";
    }
     }

预测:

@Entity
@XmlRootElement(name="forecast")
@Table(name = "forecast")
public class Forecast

{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@OneToOne(cascade = CascadeType.ALL)
private Night night;
@OneToOne(cascade = CascadeType.ALL)
private Day day;
@XmlAttribute(name = "date")
public String date;

public Long getId() {
    return id;
}


public Night getNight ()
{
    return night;
}

public void setNight (Night night)
{
    this.night = night;
}

public Day getDay ()
{
    return day;
}

public void setDay (Day day)
{
    this.day = day;
}


public void setDate (String date)
{
    this.date = date;
}

@Override
public String toString()
{
    return "ClassPojo [night = "+night+", day = "+day+", date = "+date+"]";
}

}

预测:

@Entity
@XmlRootElement(name="forecasts")
@Table(name = "forecasts")
public class Forecasts
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Forecast> forecast;

    public List<Forecast> getForecast() {
        return forecast;
    }
    @XmlElement(name = "forecast")
    public void setForecast(List<Forecast> forecast) {
        this.forecast = forecast;
    }

    public Long getId() {
        return id;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [forecast = "+ forecast +"]";
}
}

晚上:

@Entity
@XmlRootElement(name = "night")
@Table(name = "nights")
public class Night {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    private String phenomenon;
    private String tempmax;
    private String tempmin;
    private String text;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Place> places;

    public List<Place> getPlaces() {
        return places;
    }
    @XmlElement(name = "place")
    public void setPlaces(List<Place> places) {
        this.places = places;
    }



    public String getPhenomenon() {
        return phenomenon;
    }

    public void setPhenomenon(String phenomenon) {
        this.phenomenon = phenomenon;
    }

    public String getTempmax() {
        return tempmax;
    }

    public void setTempmax(String tempmax) {
        this.tempmax = tempmax;
    }

    public String getTempmin() {
        return tempmin;
    }

    public void setTempmin(String tempmin) {
        this.tempmin = tempmin;
    }

    public String getText() {
        return text;
    }

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

    @Override
    public String toString() {
        return "ClassPojo [phenomenon = " + phenomenon + ", tempmax = " + tempmax + ", tempmin = " + tempmin + ", text = " + text + "]";
    }
}

日期:

@Entity
@XmlRootElement(name="day")
@Table(name = "day")
public class Day
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    private String phenomenon;
    private String tempmax;
    private String tempmin;
    private String text;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Place> places;

    public List<Place> getPlaces() {
        return places;
    }
    @XmlElement(name = "place")
    public void setPlaces(List<Place> places) {
        this.places = places;
    }

    public String getPhenomenon ()
    {
        return phenomenon;
    }

    public void setPhenomenon (String phenomenon)
    {
        this.phenomenon = phenomenon;
    }

    public String getTempmax ()
    {
        return tempmax;
    }

    public void setTempmax (String tempmax)
    {
        this.tempmax = tempmax;
    }

    public String getTempmin ()
    {
        return tempmin;
    }

    public void setTempmin (String tempmin)
    {
        this.tempmin = tempmin;
    }

    public String getText ()
    {
        return text;
    }

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

    @Override
    public String toString()
    {
        return "ClassPojo [phenomenon = "+phenomenon+", tempmax = "+tempmax+", tempmin = "+tempmin+", text = "+text+"]";
    }
}

日期:

@Entity
@XmlRootElement(name="place")
@Table(name = "place")
public class Place
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    private String phenomenon;
    private String tempmin;
    private String tempmax;
    private String name;

    public Long getId() {
        return id;
    }

    public String getTempmax() {
        return tempmax;
    }

    public void setTempmax(String tempmax) {
        this.tempmax = tempmax;
    }

    public String getPhenomenon ()
    {
        return phenomenon;
    }

    public void setPhenomenon (String phenomenon)
    {
        this.phenomenon = phenomenon;
    }

    public String getTempmin ()
    {
        return tempmin;
    }

    public void setTempmin (String tempmin)
    {
        this.tempmin = tempmin;
    }

    public String getName ()
    {
        return name;
    }

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

    @Override
    public String toString()
    {
        return "ClassPojo [phenomenon = "+phenomenon+", tempmin = "+tempmin+", name = "+name+"]";
    }
}

application.properties

server.port=8090
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

0 个答案:

没有答案