2017-03-11 122 views
3

我的準備好的聲明有問題,但我無法弄清楚錯誤在哪裏。我正在嘗試將URI鏈接插入數據庫。如何使用JPA查詢插入數據到數據庫?

@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

不能直接使用WHERE子句INSERT聲明。你想達到什麼目的?對我來說,這種說法毫無意義。 – VimalKumar

+0

好像你應該使用'UPDATE'而不是'INSERT' –

+0

@JackFlamp我有一個更新語句,但由於在該字段中沒有任何更新,它不會執行。 –

回答

4

我設法解決這個問題。我向參數添加了一個id,以便我可以在控制器中使用Principal傳入用戶的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)執行插入,但它取決於你所使用的數據庫。下面爲我​​工作在甲骨文(使用雙表):

@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子句支持選擇支桿:http://modern-sql.com/use-case/select-without-from

相關問題