如何使用JPA Query将数据插入db?

我的预备语句有问题,但我无法弄清楚错误的位置。 我试图将URI链接插入数据库。

@Repository public interface LoggerDao extends CrudRepository { @Query("select t from Logger t where t.user.id=?#{principal.id}") List 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] ohengine.jdbc.spi.SqlExceptionHelper : SQL Error: 42001, SQLState: 42001 2017-03-11 19:52:59.157 ERROR 65154 --- [nio-8080-exec-8] ohengine.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] oaccC[.[.[/].[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.(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.(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 

我设法解决了这个问题。 我在参数中添加了一个id,以便我可以使用控制器中的Principal传入用户的id。

 @Repository public interface LoggerDao extends CrudRepository { @Query("select t from Logger t where t.user.id=?#{principal.id}") List 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); 

有一种方法可以使用obj(非本机)查询(使用@Query和@Modifying)进行插入,但这取决于您正在使用的数据库。 以下在Oracle中使用(使用Dual表):

 @Repository public interface DualRepository extends JpaRepository { @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); } 

这是一个链接,在底部显示db的支持select stmts而没有from子句: http ://modern-sql.com/use-case/select-without-from