반응형
JPA Query를 사용하여 데이터를 DB에 삽입하는 방법은 무엇입니까?
제가 준비한 명세서에 문제가 있는데 어디에 오류가 있는지 알 수가 없습니다.데이터베이스에 URI 링크를 삽입하려고 합니다.
@Repository
public interface LoggerDao extends CrudRepository<Logger, Long> {
@Query("select t from Logger t where t.user.id=?#{principal.id}")
List<Logger> findAll();
@Modifying
@Query(value = "insert into Logger t (t.redirect, t.user.id) VALUES (:insertLink,?#{principal.id})", nativeQuery = true)
@Transactional
void logURI(@Param("insertLink") String insertLink);
오류
2017-03-11 19:52:59.157 WARN 65154 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 42001, SQLState: 42001
2017-03-11 19:52:59.157 ERROR 65154 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : Syntax error in SQL statement "INSERT INTO LOGGER T[*] (T.REDIRECT, T.USER.ID) VALUES (?,?) "; expected "., (, DIRECT, SORTED, DEFAULT, VALUES, SET, (, SELECT, FROM"; SQL statement:
insert into Logger t (t.redirect, t.user.id) VALUES (?,?) [42001-190]
2017-03-11 19:52:59.181 ERROR 65154 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into Logger t (t.redirect, t.user.id) VALUES (?,?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement] with root cause
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO LOGGER T[*] (T.REDIRECT, T.USER.ID) VALUES (?,?) "; expected "., (, DIRECT, SORTED, DEFAULT, VALUES, SET, (, SELECT, FROM"; SQL statement:
insert into Logger t (t.redirect, t.user.id) VALUES (?,?) [42001-190]
at org.h2.engine.SessionRemote.done(SessionRemote.java:624) ~[h2-1.4.190.jar:1.4.190]
at org.h2.command.CommandRemote.prepare(CommandRemote.java:68) ~[h2-1.4.190.jar:1.4.190]
at org.h2.command.CommandRemote.<init>(CommandRemote.java:45) ~[h2-1.4.190.jar:1.4.190]
at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:494) ~[h2-1.4.190.jar:1.4.190]
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1188) ~[h2-1.4.190.jar:1.4.190]
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:72) ~[h2-1.4.190.jar:1.4.190]
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:276) ~[h2-1.4.190.jar:1.4.190]
at org.apache.tomcat
저는 그 문제를 해결할 수 있었습니다.컨트롤러에서 Principal을 사용하여 사용자의 id를 전달할 수 있도록 파라미터에 id를 추가했습니다.
@Repository
public interface LoggerDao extends CrudRepository<Logger, Long> {
@Query("select t from Logger t where t.user.id=?#{principal.id}")
List<Logger> findAll();
@Modifying
@Query(value = "insert into Logger (redirect,user_id) VALUES (:insertLink,:id)", nativeQuery = true)
@Transactional
void logURI(@Param("insertLink") String insertLink, @Param("id") Long id);
@Query & @Modifying을 사용하여 obj(네이티브가 아님) 쿼리를 사용하여 삽입을 수행하는 방법이 있지만 사용 중인 DB에 따라 다릅니다.아래는 Oracle에서 작업한 내용입니다(Dual table 사용).
@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);
}
하단에 from 절이 없는 db의 support select ststms를 보여주는 링크가 있습니다: http://modern-sql.com/use-case/select-without-from
언급URL : https://stackoverflow.com/questions/42739566/how-to-use-jpa-query-to-insert-data-into-db
반응형
'it-source' 카테고리의 다른 글
Postgre를 사용하여 DataJpaTest 실행SQL (0) | 2023.07.15 |
---|---|
Spring Boot에서 포함된 Tomcat 예외 처리 (0) | 2023.07.15 |
매개 변수 이름 생략, C++ 대 C (0) | 2023.07.10 |
@RestController 메서드는 기본적으로 트랜잭션인 것 같습니다. 왜죠? (0) | 2023.07.10 |
스프링 구성 클래스의 로드 순서를 적용하는 방법은 무엇입니까? (0) | 2023.07.10 |