首页 > 解决方案 > org.postgresql.util.PSQLException:错误:关系“table_name”不存在

问题描述

我正在为 Postgres GUI 使用 PostgreSQL 10、Spring Boot 2.2.7.RELEASE 和 pgAdmin 4

我有以下实体类:

@Entity
@Table(name = "\"TB03_TABLE_A\"")
public class VersionData {

    private int id;
    private String version;
    private String dataType;
    private int min;
    private int max;
    private String possibleValues;

    @Id
    @SequenceGenerator(allocationSize = 1, sequenceName = "\"TB03_TABLE_A_seq\"", name = "table_a_id_seq")
    @GeneratedValue(generator = "table_a_id_seq", strategy = GenerationType.SEQUENCE)
    @Column(name = "\"ID\"", unique = true, nullable = false)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }


    @Column(name = "\"VERSION\"")
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }

    @Column(name = "\"DATATYPE\"")
    public String getDataType() {
        return dataType;
    }
    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    @Column(name = "\"MIN\"")
    public int getMin() {
        return min;
    }
    public void setMin(int min) {
        this.min = min;
    }

    @Column(name = "\"MAX\"")
    public int getMax() {
        return max;
    }
    public void setMax(int max) {
        this.max = max;
    }

    @Column(name = "\"POSSIBLE_VALUES\"")
    public String getPossibleValues() {
        return possibleValues;
    }
    public void setPossibleValues(String possibleValues) {
        this.possibleValues = possibleValues;
    }


}

我使用 GUI pgAdmin 4 创建了表“TB03_TABLE_A”和 ID 列“TB03_TABLE_A_seq”的序列

我有以下服务类将数据保存到实体

@Service
public class VersionDataImpl{
    @Autowired
    private VersionDataRepo dataRepo;
    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void copyVersion(String newBranch, String sourceBranch) {

        copyVersionData(newBranch, sourceBranch);


    }

    @Transactional
    private void copyVersionData(String newBranch, String sourceBranch) {
        List<VersionData> versionDataLs = mddRepo.getDataByVersion(sourceBranch);

        for (VersionData  versionData : versionDataLs) {
            VersionData mdd = new VersionData();
            versionData.setDataType(mddData.getDataType());
            versionData.setMax(mddData.getMax());
            versionData.setMin(mddData.getMin());
            versionData.setPossibleValues(mddData.getPossibleValues());
            versionData.setVersion(newBranch);
            //dataRepo.insertBranchData();
            em.persist(versionData);
        }

    }

}

我有以下存储库类:

@Repository
public interface VersionDataRepo extends JpaRepository<VersionData, Integer>{

    String insertBranch = "INSERT INTO public.\"TB03_TABLE_A\"(\"VERSION\", \"DATATYPE\", \"MIN\", \"MAX\", \"POSSIBLE_VALUES\")\r\n" + 
            "VALUES ( :version, :datatype, :min, :max, :possibleValues)";

    @Query(nativeQuery = true,value = insertBranch)
    void insertBranchData();
}

如果我使用查询执行插入操作,我可以插入数据。

但是如果我在 EntityManager 中使用 Persist 方法,我会收到以下错误:

2020-05-17 20:20:27.854  WARN 25832 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2020-05-17 20:20:27.854 ERROR 25832 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "myschema.tb03_mdd_data" does not exist
  Position: 13
2020-05-17 20:20:27.865 ERROR 25832 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause

org.postgresql.util.PSQLException: ERROR: relation "tb03_mdd_data" does not exist
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2533) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2268) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:313) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159) ~[postgresql-42.2.12.jar:42.2.12]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:125) ~[postgresql-42.2.12.jar:42.2.12]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.3.jar:na]

我哪里错了?请帮忙!

提前致谢!

标签: postgresqlhibernatespring-bootspring-data-jpa

解决方案


我之前也有同样的问题。最好的办法是使用您要创建的表名创建一个类。这是因为,您的表名和类名不同。只需删除@Table注释并使用类名作为表名。spring-boot 本身未检测到此注释。结果没有使用它的意义。我认为,必须在您的数据库上创建 名称为version_data的表。


推荐阅读