加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在spring-data中插入db?

发布时间:2020-12-15 01:48:17 所属栏目:大数据 来源:网络整理
导读:我想发出一个将数据插入我的数据库的请求.该表有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

我想发出一个将数据插入我的数据库的请求.该表有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)

因此在您的控制器中只需创建对象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);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读