2017-03-11 15 views
3

私は準備した明細書に問題がありますが、どこにエラーがあるのか​​わかりません。私はデータベースにURIリンクを挿入しようとしています。私はこの問題を解決するために管理JPA Queryを使用してデータベースにデータを挿入する方法は?

@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 
+0

ステートメント。あなたは何を達成しようとしていますか?私にはこの声明は意味をなさない。 – VimalKumar

+0

は 'INSERT'の代わりに' UPDATE'を使うべきです。 –

+0

@JackFlamp私は更新ステートメントを持っていますが、更新するフィールドには何もないので実行されません。 –

答えて

4

。コントローラの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); 
0

は、OBJ(ネイティブではない)クエリ(@query & @Modifyingを使用)を使用して挿入を行う方法があるが、それはあなたが使用しているDBに依存します。以下は、(デュアルテーブルを使用して)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); 
} 

はここ句からずに下デシベルのサポートを選択STMTSで示したリンクです:http://modern-sql.com/use-case/select-without-fromあなたはINSERTと直接WHERE句を使用することはできません

関連する問題