I was trying to run API using Spring Framework. While working on it I got above error message when I tried running endpoints. Below is the class that I tried testing on Postman. In which I have used sessions table from the PostgreSQL data base
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;
}
}
And the Error message that I got in Postman is
{
"timestamp": "2022-01-03T21:28:36.793+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/v1/sessions"
}
While Error message in IntelliJ when I tried GET request from Postman is:
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
This Looks very similar to the Spring Boot tutorial I've done recently. One thing to make sure of is that the table already exists in the database schema. The way the model is setup it does not automatically create the table in the database when the api is run.
Another thing to check is your application.properties settings. The "spring.datasource.url" option should be set to the correct database it is looking for. ie: "jdbc:postgresql://localhost:xxxx/app_name" where app_name is what you named the database and xxxx is the port.
I ran into this similar issue with the many to many relationship for sessions and speakers, and the culprit was the missing table in the database.