o.h.engine.jdbc.spi.SqlExceptionHelper : 错误: 关系"会话"不存在



我试图使用Spring Framework运行API。在处理它的过程中,当我尝试运行端点时,我得到了上面的错误消息。下面是我尝试在Postman上测试的课程。在其中,我使用了PostgreSQL数据库中的会话

package com.pluralsight.conferencedemo.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.util.List;
@Entity(name="sessions")
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long session_id;
private String session_name;
private String session_description;
private Integer session_length;
@ManyToMany
@JoinTable(
name="session_speakers",
joinColumns = @JoinColumn(name = "session_id"),
inverseJoinColumns = @JoinColumn(name= "speaker_id"))
private List<Speaker> speakers;
public Session(){
}
public List<Speaker> getSpeakers() {
return speakers;
}
public void setSpeakers(List<Speaker> speakers) {
this.speakers = speakers;
}
public Long getSession_id() {
return session_id;
}
public void setSession_id(Long session_id) {
this.session_id = session_id;
}
public String getSession_name() {
return session_name;
}
public void setSession_name(String session_name) {
this.session_name = session_name;
}
public String getSession_description() {
return session_description;
}
public void setSession_description(String session_description) {
this.session_description = session_description;
}
public Integer getSession_length() {
return session_length;
}
public void setSession_length(Integer session_length) {
this.session_length = session_length;
}
}

我在邮差中收到的错误消息是

{
"timestamp": "2022-01-03T21:28:36.793+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/v1/sessions"
}

当我尝试Postman的GET请求时,IntelliJ中的错误消息是:

2022-01-04 02:58:36.695  WARN 10200 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2022-01-04 02:58:36.695 ERROR 10200 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "sessions" does not exist
Position: 179
2022-01-04 02:58:36.758 ERROR 10200 --- [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 org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: ERROR: relation "sessions" does not exist

这看起来与我最近完成的Spring Boot教程非常相似。需要确保的一件事是该表已经存在于数据库模式中。按照模型的设置方式,运行api时不会自动在数据库中创建表。

另一件需要检查的事情是你的application.properties设置。";spring.datasource.url";选项应该设置为它正在查找的正确数据库。即:";jdbc:postgresql://localhost:xxxx/app_name"其中app_name是数据库的名称,xxxx是端口。

我在会议和发言人的多对多关系中遇到了类似的问题,罪魁祸首是数据库中缺少的表。

我也有同样的错误,但愚蠢的错误是

在创建";conference_app"来自CLI的数据库,我忘记连接它并创建了所有的表。

只需在创建后先连接数据库(通过以下命令(

c conference_app

然后创建会话和演讲者表。

最新更新