I want to run an Update
query using createNativeQuery
in entityManager. I am not able to run it.
My class structure :
class ABC_DAO
{
List<a> = entityManager.createNativeQuery(select.......); //sql1 : it is working fine
Sysout("some value"); // it is working
entityManager.createNativeQuery(update.......);// ***sql2 :it is not working***
Sysout("some value"); // it is working
}
Hibernate is not executing sql2
but executing sql2
. We are using Postgres db. This query has to be in Sql. We are using Hibernate with JPA.
Let my try to help you on behalf of your erroneous code example and problem description.
1) You will only get a List
as result of a query if you call getResultList()
on it, otherwise sql1
would not work (Please post the complete code, if you want to get help) :
List<a> = entityManager.createNativeQuery("sql1", a.class).getResultList();
2) For update statements you have to call the method executeUpdate()
and not getResultList()
(or getSingleResult()
)to send the native SQL statement to the database:
int countUpdated = entityManager.createNativeQuery("sql2").executeUpdate();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.