如何在JPA中构建插入查询 [英] How to construct an insert query in JPA

查看:311
本文介绍了如何在JPA中构建插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入到列(NAME,VALUE)

  Query query = em.createQuery(INSERT INTO TestDataEntity(NAME,VALUE)VALUES(:name,:value)); 
query.setParameter(name,name);
query.setParameter(value,value);
query.executeUpdate();

并获得以下例外:

  ERROR org.hibernate.hql.internal.ast.ErrorCounter  -  line 1:42:unexpected token:VALUES 

另外,我无法使用本机查询插入记录:

 查询查询= em.createNativeQuery(INSERT INTO TEST_DATA(NAME,VALUE)VALUES(:name,:value);); 
query.setParameter(name,name);
query.setParameter(value,value);
query.executeUpdate();

正在抛出另一个例外情况:

 javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法执行语句

问题是:


  • 查询字符串有什么问题?



非常感谢。

解决方案

这个问题。



根据这个


JPA中没有INSERT语句。


但是我可以用本地查询来解决这个问题:我错误地把冗余;在查询结束时,通过删除它来解决问题。


I am trying to insert data into a table having columns (NAME, VALUE) with

Query query = em.createQuery("INSERT INTO TestDataEntity (NAME, VALUE) VALUES (:name, :value)");
query.setParameter("name", name);
query.setParameter("value", value);
query.executeUpdate();

and getting the following exception:

ERROR org.hibernate.hql.internal.ast.ErrorCounter - line 1:42: unexpected token: VALUES 

Also, I cannot insert a record using a native query either:

Query query = em.createNativeQuery("INSERT INTO TEST_DATA (NAME, VALUE) VALUES (:name, :value);");
query.setParameter("name", name);
query.setParameter("value", value);
query.executeUpdate();

Another exception is being thrown:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement

The question is:

  • What is wrong with the query string?

Many thanks.

解决方案

I solved the issue.

According to this,

There is no INSERT statement in JPA.

But I could solve the issue with native query: I have mistakenly put a redundant ; at the end of the query, so the issue solved by removing it.

这篇关于如何在JPA中构建插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆