简体   繁体   English

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: 无法使用 executeQuery() 发出数据操作语句

[英]javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Can not issue data manipulation statements with executeQuery()

I am insert data in db is good.我在数据库中插入数据很好。 The problem is after inserted data i am trying to get last inserted data primary key column.问题是在插入数据后我试图获取最后插入的数据主键列。 But i can't how to solve this problem.但我无法解决这个问题。

note: i am using jpql, Thanks注意:我正在使用 jpql,谢谢

public void insertData(String hcpHceId) {
        EntityManager em = getEntityManager();

            em.getTransaction().begin();
//          Query query = em.createNativeQuery("INSERT INTO mdm_id (hcp_hce_id)  VALUES(:id)" );
            Query query = em.createNativeQuery("INSERT INTO mdm_id (hcp_hce_id)  VALUES(?)");
            query.setParameter(1, hcpHceId);
            query.executeUpdate(); // Successfully inserted data
            List list = query.setMaxResults(1).getResultList();  // Error line
            em.getTransaction().commit();

        } catch (Exception error) {
            error.printStackTrace();
            logger.error(error.getMessage());
        } finally {
            em.close();;
        }


}

You are trying to retrieve the list from the same execute query.您正在尝试从同一个执行查询中检索列表。 Query is an immutable object.查询是不可变的 object。 You must create a new query with the proper select statement, so you will be able to retrieve data from database.您必须使用正确的 select 语句创建一个新查询,这样您才能从数据库中检索数据。

After the executeUpdate, create the new query using, vg (as you did not show your entity graph, using native sql):在 executeUpdate 之后,使用 vg 创建新查询(因为您没有显示实体图,使用本机 sql):

Query retQuery = em.createNativeQuery("SELECT hcp_hce_id FROM mdm_id LIMIT 1");
String id = (String) retQuery.getSingleResult();
assert(id == hcpHceId);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java- javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法执行语句 - java- javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException:could not execute statement javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法使用oracle执行查询 - javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query with oracle Spring boot 2.1.9 - CriteriaBuilder javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: 无法准备语句 - Spring boot 2.1.9 - CriteriaBuilder javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement org.hibernate.exception.SQLGrammarException: 无法准备语句; 嵌套异常是 javax.persistence.PersistenceException - org.hibernate.exception.SQLGrammarException: could not prepare statement; nested exception is javax.persistence.PersistenceException 线程“main”中的异常 javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 无法执行语句 - Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement 线程“主”javax.persistence.PersistenceException 中的异常:org.hibernate.exception.ConstraintViolationException:无法执行语句 - Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:无法执行查询 - javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCExcep tion: could not execute query javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法执行查询 - javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query javax.persistence.PersistenceException:org.hibernate.id.IdentifierGenerationException: - javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:error - javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM