【发布时间】:2020-09-08 01:12:48
【问题描述】:
我只是尝试从 Heroku 中托管的应用程序发布和获取数据。但是我得到了这个错误,
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu May 21 11:27:53 UTC 2020
There was an unexpected error (type=Internal Server Error, status=500).
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
我在我的本地主机上使用链接 Heroku Postgresql 数据库对此进行了测试,并且没有出现任何错误,并且在 post 和 get 都成功提交。上述错误仅在尝试通过 Heroku 地址获取或发布数据时发生。
以下是代码段,
实体类
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;
@Entity
@Table(name = "notes")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String title;
@NotBlank
private String content;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
控制器类
import com.mcq.webapp.exception.ResourceNotFoundException;
import com.mcq.webapp.model.Note;
import com.mcq.webapp.repository.NoteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/api")
public class NoteController {
@Autowired
NoteRepository noteRepository;
@GetMapping("/notes")
public List<Note> getAllNotes() {
return noteRepository.findAll();
}
@PostMapping("/notes")
public Note createNote(@Valid @RequestBody Note note) {
return noteRepository.save(note);
}
@GetMapping("/notes/{id}")
public Note getNoteById(@PathVariable(value = "id") Long noteId) {
return noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
}
@PutMapping("/notes/{id}")
public Note updateNote(@PathVariable(value = "id") Long noteId,
@Valid @RequestBody Note noteDetails) {
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
note.setTitle(noteDetails.getTitle());
note.setContent(noteDetails.getContent());
Note updatedNote = noteRepository.save(note);
return updatedNote;
}
@DeleteMapping("/notes/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
noteRepository.delete(note);
return ResponseEntity.ok().build();
}
}
存储库
import com.mcq.webapp.model.Note;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface NoteRepository extends JpaRepository<Note, Long> {
}
在这种情况下,同一个项目同一个数据库,只是环境不同。因此,这是什么原因。该程序在 localhost 中运行,但不在 Heroku 环境中运行。
谢谢
【问题讨论】:
-
您确定您的托管数据库配置正确吗?表已经创建和一切?还要检查你的 app.properties,尤其是 ddl-auto。
-
@Maaaatt 感谢您的评论。是的,我添加了如下,SPRING_JPA_HIBERNATE_DDL-AUTO = update
-
如果架构尚未创建,我相信它 (ddl-auto:update) 不会为您创建它。可能值得通过客户端连接并检查数据库上的内容。
-
谢谢@Maaaatt,我解决了我的问题。我更新为这个 spring.jpa.hibernate.ddl-auto = create
标签: java hibernate spring-boot heroku