我正在尝试将Excel文件保存到我的数据库中.我得到了这个错误:
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: operator does not exist: character varying = bytea
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
这是我的控制器post方法:
@PostMapping
public ResponseEntity<DocumentCreateResponse> addDocument(@RequestParam("file") MultipartFile file) throws IOException {
Path copyLocation = Paths.get(uploadDir, Objects.requireNonNull(file.getOriginalFilename()));
Files.copy(file.getInputStream(), copyLocation, StandardCopyOption.REPLACE_EXISTING);
Document savedDocument = documentService.save(copyLocation.toAbsolutePath().toFile());
if (savedDocument == null){
return new ResponseEntity<>(new DocumentCreateResponse(null), null, HttpStatus.NOT_ACCEPTABLE);
}
return new ResponseEntity<>(new DocumentCreateResponse(savedDocument.getId()), null, HttpStatus.ACCEPTED);
}
这是document类:
@Entity
public class DocumentEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="document_id")
private Long id;
@Column(name ="document_file")
private File file;
public DocumentEntity() {
}
public DocumentEntity(File file) {
this.file = file;
}
}
这是我的模式创建文件(我使用Flyway和PostgreSQL)
create table if not exists document_entity
(
document_id bigserial
constraint document_entity_document_id
not null primary key,
document_file varchar not null
);
我应该在哪里添加更改,以便正确保存我的文件,而不会导致此错误?我已经阅读了文章并看到了这个错误,但是我不知道我应该在我的代码中修改什么。
public class DocumentService implements DocumentServicePort {
private final DocumentRepository documentRepository;
public DocumentService(DocumentRepository documentRepository) {
this.documentRepository = documentRepository;
}
@Override
public Document save(File file) {
Document document = new Document(file);
if (document.hasValidExtension(document)) {
documentRepository.saveDocument(document);
return document;
}
return null;
}
}
发布于 2021-08-11 17:20:10
您已在数据库中将文件声明为varchar,而在应用程序中已将该字段声明为File。
您可以做一些事情,但是对于您提到的情况,请更改数据库中列的类型。它应该是'bytea`
在db上运行此命令(如果表为空会更好):
ALTER TABLE document_entity ALTER COLUMN document_file TYPE bytea USING document_file::TEXT::BYTEA;
https://stackoverflow.com/questions/68745887
复制相似问题