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

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

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

问题

这里是您的翻译内容:

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

这是我的实体类Product.java:

  1. @Entity
  2. @Table(name = "products")
  3. public class Product {
  4. @Id
  5. @Column(name = "product_id")
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. private long product_id;
  8. @Column(name = "product_name")
  9. private String productName;
  10. @Column(name = "image_url")
  11. private String imageURL;
  12. @Column(name = "close_date")
  13. private LocalDate closeDate;
  14. @Column(name = "deliver_date")
  15. private LocalDate deliverDate;
  16. @Column(name = "slot")
  17. private int slot;
  18. @Column(name = "price")
  19. private double price;
  20. @Column(name = "seller_id")
  21. private long sellerId;
  22. public Product(String productName, String image, LocalDate closeDate, LocalDate deliverDate, int slot, double price, long sellerId){
  23. this.productName = productName;
  24. this.imageURL = image;
  25. this.closeDate = closeDate;
  26. this.deliverDate = deliverDate;
  27. this.slot = slot;
  28. this.price = price;
  29. this.sellerId = sellerId;
  30. }
  31. }

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

  1. # JPA
  2. spring.jpa.hibernate.ddl-auto=none
  3. spring.jpa.open-in-view=false
  4. spring.datasource.username=postgres
  5. spring.datasource.password=//pass
  6. #Logging
  7. spring.jpa.show-sql=true
  8. spring.jpa.properties.hibernate.format_sql=true
  9. #GCP
  10. spring.cloud.gcp.sql.database-name=warehouse
  11. spring.cloud.gcp.sql.instance-connection-name=//pid:asia-southeast2:warehouse
  12. spring.datasource.initialization-mode=always
  13. spring.cloud.gcp.project-id=//pid
  14. spring.cloud.gcp.credentials.location=//cred

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

  1. @GetMapping("/test")
  2. public String test(){
  3. repo.save(prod);
  4. return "added";
  5. }

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

  1. 2020-08-26 00:10:47.326 INFO 8924 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
  2. Hibernate:
  3. insert
  4. into
  5. products
  6. (close_date, deliver_date, image_url, price, product_name, seller_id, slot)
  7. values
  8. (?, ?, ?, ?, ?, ?, ?)
  9. 2020-08-26 00:10:47.685 WARN 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
  10. 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
  11. Position: 13
  12. 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
  13. org.postgresql.util.PSQLException: ERROR: relation "products" does not exist
  14. Position: 13

我的表格确实存在:

  1. postgres=> SELECT * FROM products
  2. postgres-> ;
  3. product_id | product_name | image_url | close_date | deliver_date | slot | price | seller_id
  4. ------------+--------------+-----------+------------+--------------+------+-------+-----------
  5. (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

  1. @Entity
  2. @Table(name = "products")
  3. public class Product {
  4. @Id
  5. @Column(name = "product_id")
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. private long product_id;
  8. @Column(name = "product_name")
  9. private String productName;
  10. @Column(name = "image_url")
  11. private String imageURL;
  12. @Column(name = "close_date")
  13. private LocalDate closeDate;
  14. @Column(name = "deliver_date")
  15. private LocalDate deliverDate;
  16. @Column(name = "slot")
  17. private int slot;
  18. @Column(name = "price")
  19. private double price;
  20. @Column(name = "seller_id")
  21. private long sellerId;
  22. public Product(String productName, String image, LocalDate closeDate, LocalDate deliverDate, int slot, double price, long sellerId){
  23. this.productName = productName;
  24. this.imageURL = image;
  25. this.closeDate = closeDate;
  26. this.deliverDate = deliverDate;
  27. this.slot = slot;
  28. this.price = price;
  29. this.sellerId = sellerId;
  30. }
  31. }

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.

  1. # JPA
  2. spring.jpa.hibernate.ddl-auto=none
  3. spring.jpa.open-in-view=false
  4. spring.datasource.username=postgres
  5. spring.datasource.password=//pass
  6. #Logging
  7. spring.jpa.show-sql=true
  8. spring.jpa.properties.hibernate.format_sql=true
  9. #GCP
  10. spring.cloud.gcp.sql.database-name=warehouse
  11. spring.cloud.gcp.sql.instance-connection-name=//pid:asia-southeast2:warehouse
  12. spring.datasource.initialization-mode=always
  13. spring.cloud.gcp.project-id=//pid
  14. spring.cloud.gcp.credentials.location=//cred

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

  1. @GetMapping("/test")
  2. public String test(){
  3. repo.save(prod);
  4. return "added";
  5. }

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

  1. 2020-08-26 00:10:47.326 INFO 8924 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
  2. Hibernate:
  3. insert
  4. into
  5. products
  6. (close_date, deliver_date, image_url, price, product_name, seller_id, slot)
  7. values
  8. (?, ?, ?, ?, ?, ?, ?)
  9. 2020-08-26 00:10:47.685 WARN 8924 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
  10. 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
  11. Position: 13
  12. 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
  13. org.postgresql.util.PSQLException: ERROR: relation "products" does not exist
  14. Position: 13
  15. at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) ~[postgresql-42.2.14.jar:42.2.14]
  16. at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267) ~[postgresql-42.2.14.jar:42.2.14]
  17. at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312) ~[postgresql-42.2.14.jar:42.2.14]
  18. at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.14.jar:42.2.14]
  19. at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.14.jar:42.2.14]
  20. at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153) ~[postgresql-42.2.14.jar:42.2.14]
  21. at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:119) ~[postgresql-42.2.14.jar:42.2.14]
  22. at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.5.jar:na]
  23. at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.5.jar:na]
  24. 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

  1. postgres=> SELECT * FROM products
  2. postgres-> ;
  3. product_id | product_name | image_url | close_date | deliver_date | slot | price | seller_id
  4. ------------+--------------+-----------+------------+--------------+------+-------+-----------
  5. (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:

确定