【问题标题】:org.postgresql.util.PSQLException: ERROR: relation "table_name" does not existorg.postgresql.util.PSQLException:错误:关系“table_name”不存在
【发布时间】:2020-09-03 07:50:36
【问题描述】:

我正在为 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 为 ID 列“TB03_TABLE_A_seq”创建了表“TB03_TABLE_A”和序列

我有以下服务类将数据持久化到实体

@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]

我哪里出错了?请帮忙!

提前致谢!

【问题讨论】:

标签: postgresql hibernate spring-boot spring-data-jpa


【解决方案1】:

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

【讨论】:

  • 我尝试使用我在 Postgres 中创建的表名创建类名。我仍然面临“关系不存在”的错误
  • 那么这个错误一定是在另一个类中。删除所有类的@Table 注解并重新运行程序。这不是创建表的原因。它仍然使用类名在数据库中创建表。
【解决方案2】:

您正在使用架构“公共”。在您的原始 SQL 中,而您使用的是“myschema”。在 ORM 中。

检查您的连接设置 - 您可能没有提供架构(例如,可能试图插入到“postgres”架构中。

如果您使用的是管理员帐户(您绝对不应该使用该帐户),您可能使用了错误的架构。与此相关,您可以为您的用户提供默认架构,否则您需要切换架构等。

以编程方式找出答案的一种方法 - 例如((SessionFactoryImplementor)sessionFactory).getSettings().getDefaultSchemaName();

【讨论】:

  • 很抱歉给您带来不便!我现在已经编辑了我的错误跟踪。以前我尝试通过提供 Schema 并添加了该错误跟踪。即使删除架构后,我也会遇到同样的错误
猜你喜欢
  • 1970-01-01
  • 2017-04-11
  • 2015-10-09
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 2012-01-13
相关资源
最近更新 更多