ERROR: 表 “products” 不存在 Cloud SQL Postgres + Spring Data

huangapple go评论70阅读模式
英文:

ERROR: relation "products" does not exist Cloud SQL Postgres + Spring Data

问题

这里是您的翻译内容:

我正在尝试使用Spring Data连接GCP SQL。我已成功连接到数据库。但是,我不确定为什么Hibernate似乎无法识别现有的表格。

这是我的实体类Product.java:

@Entity
@Table(name = "products")
public class Product {
    @Id
    @Column(name = "product_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long product_id;
    @Column(name = "product_name")
    private String productName;
    @Column(name = "image_url")
    private String imageURL;
    @Column(name = "close_date")
    private LocalDate closeDate;
    @Column(name = "deliver_date")
    private LocalDate deliverDate;
    @Column(name = "slot")
    private int slot;
    @Column(name = "price")
    private double price;
    @Column(name = "seller_id")
    private long sellerId;

    public Product(String productName, String image, LocalDate closeDate, LocalDate deliverDate, int slot, double price, long sellerId){
        this.productName = productName;
        this.imageURL = image;
        this.closeDate = closeDate;
        this.deliverDate = deliverDate;
        this.slot = slot;
        this.price = price;
        this.sellerId = sellerId;
    }
}

这是我的application.properties。SQL实例名为"warehouse",数据库也名为"warehouse",而表格名为"products"。

# JPA
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false

spring.datasource.username=postgres
spring.datasource.password=//pass

#Logging
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

#GCP
spring.cloud.gcp.sql.database-name=warehouse
spring.cloud.gcp.sql.instance-connection-name=//pid:asia-southeast2:warehouse
spring.datasource.initialization-mode=always
spring.cloud.gcp.project-id=//pid
spring.cloud.gcp.credentials.location=//cred

这是我用来测试代码的测试控制器:

@GetMapping("/test")
public String test(){
    repo.save(prod);
    return "added";
}

我真的不明白我在这里做错了什么:

2020-08-26 00:10:47.326 INFO 8924 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
Hibernate:
insert
into
products
(close_date, deliver_date, image_url, price, product_name, seller_id, slot)
values
(?, ?, ?, ?, ?, ?, ?)
2020-08-26 00:10:47.685 WARN 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2020-08-26 00:10:47.685 ERROR 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "products" does not exist
Position: 13
2020-08-26 00:10:47.834 ERROR 8924 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause

org.postgresql.util.PSQLException: ERROR: relation "products" does not exist
Position: 13

我的表格确实存在:

postgres=> SELECT * FROM products
postgres-> ;
 product_id | product_name | image_url | close_date | deliver_date | slot | price | seller_id 
------------+--------------+-----------+------------+--------------+------+-------+-----------
(0 rows)

我真的感觉像是撞到了一个硬伤,我不明白出了什么问题,哈哈。

英文:

I am trying to connect GCP SQL with spring data. I have had success connecting to the database. However, I am not sure why hibernate doesn't seem to recognise the existing table.

Here is my Entity class product.java

@Entity
@Table(name = "products")
public class Product {
    @Id
    @Column(name = "product_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     private long product_id;
    @Column(name = "product_name")
     private String productName;
    @Column(name = "image_url")
     private String imageURL;
    @Column(name = "close_date")
     private LocalDate closeDate;
    @Column(name = "deliver_date")
     private LocalDate deliverDate;
    @Column(name = "slot")
     private int slot;
    @Column(name = "price")
     private double price;
    @Column(name = "seller_id")
     private long sellerId;

    public Product(String productName, String image, LocalDate closeDate, LocalDate deliverDate, int slot, double price, long sellerId){
        this.productName = productName;
        this.imageURL = image;
        this.closeDate = closeDate;
        this.deliverDate = deliverDate;
        this.slot = slot;
        this.price = price;
        this.sellerId = sellerId;
    }
}

Here is my application.properties The sql instance is named warehouse and the database is named warehouse as well while the warehouse is named products.

    # JPA
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false

spring.datasource.username=postgres
spring.datasource.password=//pass

#Logging
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

#GCP
spring.cloud.gcp.sql.database-name=warehouse
spring.cloud.gcp.sql.instance-connection-name=//pid:asia-southeast2:warehouse
spring.datasource.initialization-mode=always
spring.cloud.gcp.project-id=//pid
spring.cloud.gcp.credentials.location=//cred

Here is my test controller on which i test the code:

@GetMapping("/test")
public String test(){
    repo.save(prod);
    return "added";
}

I really do not understand what have I been doing wrong here:

    2020-08-26 00:10:47.326  INFO 8924 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
Hibernate: 
    insert 
    into
        products
        (close_date, deliver_date, image_url, price, product_name, seller_id, slot) 
    values
        (?, ?, ?, ?, ?, ?, ?)
2020-08-26 00:10:47.685  WARN 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2020-08-26 00:10:47.685 ERROR 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "products" does not exist
  Position: 13
2020-08-26 00:10:47.834 ERROR 8924 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause

org.postgresql.util.PSQLException: ERROR: relation "products" does not exist
  Position: 13
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153) ~[postgresql-42.2.14.jar:42.2.14]
        at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:119) ~[postgresql-42.2.14.jar:42.2.14]
        at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.5.jar:na]
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.5.jar:na]
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]

My table do exists

postgres=> SELECT * FROM products
postgres-> ;
 product_id | product_name | image_url | close_date | deliver_date | slot | price | seller_id 
------------+--------------+-----------+------------+--------------+------+-------+-----------
(0 rows)

I really feel like hitting a bedrock, I don't understand what is wrong haha

答案1

得分: 2

问题是我在 PostgreSQL 默认数据库而不是数据仓库上创建了表。非常抱歉浪费了您的时间。感谢您的阅读!

英文:

Turns out the silly problem was i created the table on postgres default database instead of warehouse. I am very sorry for wasting your time. Thank you for reading through!

huangapple
  • 本文由 发表于 2020年8月25日 22:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定