当前位置: 首页 > 知识库问答 >
问题:

如何使用JPA查询将数据插入数据库?

朱啸
2023-03-14

我的准备好的语句有问题,但我无法找出错误所在。我正在尝试将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

共有2个答案

索寒
2023-03-14

有一种方法可以使用obj(非本机)查询(使用@Query

@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子句的stmts:http://modern-sql.com/use-case/select-without-from

林德惠
2023-03-14

我设法解决了这个问题。我在参数中添加了一个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);
 类似资料:
  • 我有这样的查询(select id,reffid,someData from myTable): 我需要查询来执行以下操作:如果我有少于 4 个具有相同值的 reffid,则添加具有相同 reffid 的行并在 someData 中添加 -。结果查询应如下所示: 如何做到这一点?

  • 问题内容: 我想在我的MySQL数据库中插入整数188和90,但以下代码不起作用: 为什么不起作用? 问题答案: 编辑 为我工作: 在MySQL表;

  • 问题内容: 从Python脚本向MySQL插入一些数据时,出现一个奇怪的错误。这基本上与我要插入的变量为空有关。我认为MySQL不喜欢空白变量,但是还有其他我可以将其更改为可以与我的insert语句一起使用的东西吗? 我可以成功地使用一条语句将其设置为0(如果为空),但这可能会弄乱我计划稍后在MySQL中进行的某些数据分析。有没有办法将其转换为或某种方式,以便MySQL接受但不添加任何内容? 问题

  • 我有一个电子邮件订阅我的网站,我想插入到我的Wordpress数据库,所以我可以导出电子邮件列表。我已经创建了表wp_email_subscription,其中包含4个字段ID、名称、电子邮件和创建日期。对此将进行什么查询?有没有wordpress数据库脚本可以使用?

  • 我正在尝试通过我的应用程序将数据插入Google Cloud mysql数据库,该应用程序是通过eclipse构建在java上的。这是代码: 部署项目后,我遇到以下错误: java.sql.SQLException:没有找到适合jdbc: mysql://google/eduvitae? cloud dSqlInstance=edu-vitae1的驱动程序 谁能帮我解决这个问题。提前谢谢

  • 问题内容: 我有一个输出当前数据的现有查询,我想将其插入到Temp表中,但是这样做有一些问题。有人会对如何执行此操作有一些见解吗? 这是一个例子 这似乎当前以我需要的方式输出我的数据,但我想将其传递到临时表中。我的问题是我对SQL查询还很陌生,还没有找到一种方法来做到这一点。或者,即使有可能。如果不可能,是否有更好的方法将我要查找的数据获取到临时表中? 问题答案: