[英]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.