spring 如何在spring-data中插入db?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32989426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:48:47  来源:igfitidea点击:

How to insert into db in spring-data?

springspring-data

提问by que1326

I want to make a request that inserts data into my database. The table has 4 columns: ID_DOCUMENT (PK), ID_TASK, DESCRIPTION, FILEPATH

我想发出一个将数据插入我的数据库的请求。该表有 4 列:ID_DOCUMENT (PK)、ID_TASK、DESCRIPTION、FILEPATH

Entity

实体

... 
@Column(name = "ID_TASK")
private Long idTask;


@Column(name = "DESCRIPTION")
private String description;

@Column(name = "FILEPATH")
private String filepath;
...

Repository

存储库

@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);

Controller

控制器

@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);
}

When I run my test, I get this error: Caused by: org.hibernate.hql.ast.QuerySyntaxException: expecting OPEN, found 'c' near line 1, column 32 [insert into TaskDocumentEntity c (c.idTask, c.descriere, c.filepath) values (:id,:descriere,:filepath)]I tried to remove the alias c, and still doesn`t work.

当我运行我的测试时,我得到这个错误: 由:org.hibernate.hql.ast.QuerySyntaxException:期望打开,在第 1 行,第 32 列附近找到“c”[插入到 TaskDocumentEntity c (c.idTask, c.descriere , c.filepath) values (:id,:descriere,:filepath)]我试图删除别名 c,但仍然不起作用。

回答by pezetem

Spring data provides out of the box savemethod used for insertion to database - no need to use @Query. Take a look at core concepts of springData (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts)

Spring data 提供了save用于插入数据库的开箱即用的方法 - 无需使用@Query. 查看 springData 的核心概念(http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts

thus in your controller just create object TaskDocumentEntityand pass it to repository

因此在您的控制器中只需创建对象TaskDocumentEntity并将其传递给存储库

@RequestMapping(value = "/services/tasks/addDocument", method = RequestMethod.POST)
@ResponseBody
public void set(@RequestParam("idTask") Long idTask,@RequestParam("description") String description,@RequestParam("filepath") String filepath){

// assign parameters to taskDocumentEntity by constructor args or setters
        TaskDocumentEntity document = new TaskDocumentEntity(idTask,descriere,filepath);
        taskDocumentRepository.save(document);
    }

回答by A.P

There is a way to do this but it depends on the db you're using. Below worked for me in Oracle (using Dual table):

有一种方法可以做到这一点,但这取决于您使用的数据库。下面在 Oracle 中对我来说有效(使用双表):

@Repository
public interface DualRepository extends JpaRepository<Dual,Long> {
    @Modifying
    @Query("insert into Person (id,name,age) select :id,:name,:age from Dual")
    public int modifyingQueryInsertPerson(@Param("id")Long id, @Param("name")String name, @Param("age")Integer age);
}

So in your case, it would be (if Oracle):

因此,在您的情况下,它将是(如果是 Oracle):

@Modifying
@Query("insert into TaskDocumentEntity (idTask,description,filepath) select :idTask,:description,:filepath from Dual")
public void insertDocumentByTaskId(@Param("idTask") Long id,@Param("description") String description,@Param("filepath") String filepath)

I'm not sure which db you're using, here's a link which shows at the bottom which db's support select stmts without a from clause : http://modern-sql.com/use-case/select-without-from

我不确定您使用的是哪个 db,这里有一个链接,它在底部显示哪个 db 支持 select stmts 没有 from 子句:http://modern-sql.com/use-case/select-without-from