我想发出一个将数据插入我的数据库的请求.该表有4列:ID_DOCUMENT(PK),ID_TASK,DESCRIPTION,FILEPATH
实体
...
@Column(name = "ID_TASK")
private Long idTask;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "FILEPATH")
private String filepath;
...
知识库
@Modifying
@Query("insert into TaskDocumentEntity c (c.idTask,c.description,c.filepath) values (:id,:description,:filepath)")
public void insertDocumentByTaskId(@Param("id") Long id,@Param("description") String description,@Param("filepath") String filepath);
调节器
@RequestMapping(value = "/services/tasks/addDocument",method = RequestMethod.POST)
@ResponseBody
public void set(@RequestParam("idTask") Long idTask,@RequestParam("description") String description,@RequestParam("filepath") String filepath){
//TaskDocumentEntity document = new TaskDocumentEntity();
taskDocumentRepository.insertDocumentByTaskId(idTask,descriere,filepath);
}
当我运行我的测试时,我收到此错误:
引起:org.hibernate.hql.ast.QuerySyntaxException:期待OPEN,在第1行第32列附近找到’c'[插入TaskDocumentEntity c(c.idTask,c.descriere,c.filepath)值(:id,: descriere,:文件路径)]
我试图删除别名c,但仍然无法正常工作.
最佳答案
Spring数据提供了用于插入数据库的开箱即用的保存方法 – 无需使用@Query.看一下springData的核心概念(http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts)
原文链接:https://www.f2er.com/spring/432685.html因此在您的控制器中只需创建对象TaskDocumentEntity并将其传递给存储库
@RequestMapping(value = "/services/tasks/addDocument",@RequestParam("filepath") String filepath){
// assign parameters to taskDocumentEntity by constructor args or setters
TaskDocumentEntity document = new TaskDocumentEntity(idTask,filepath);
taskDocumentRepository.save(document);
}