How to insert in db in spring-data?

I want to make a query that inserts data into my database. The table has 4 columns: 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: Reason: org.hibernate.hql.ast.QuerySyntaxException: OPEN expected, found "c" next to line 1, column 32 [insert into values ​​TaskDocumentEntity c (c.idTask, c. descriere, c.filepath) (: id ,: description ,: FilePath)] I tried to remove the alias c and still does not work.

+4
source share
2 answers

Spring save , - @Query. springData (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts)

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);
    }
+5

, db. Oracle ( Dual):

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

, ( 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)

, db , , , db, stmts from: http://modern-sql.com/use-case/select-without-from

+2

Source: https://habr.com/ru/post/1610560/


All Articles