I want to call named query that performs UPDATE sql query.
Named query Topic.updateViewQuantity is as follows:
UPDATE Topic t SET t.viewQuantity=t.viewQuantity + 1 WHERE t.id = :topicId[/code]
I have session scoped facade that uses container-managed transaction. The result of calling query.executeUpdate is :
Caused by: javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager at com.sun.enterprise.container.common.impl.QueryWrapper.executeUpdate(QueryWrapper.java:225)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
I tried to put query.executeUpdate in separate method and call the whole method within utx.begin and utx.commit but unfortunately it does not help.
I know exactly utx.begin() works well because utx.getStatus() returns 0 which means that transaction is active.
If I remove utx.begin and utx.commit the same exception is thrown.
How to start container-managed transaction before calling executeUpdate()?
How to execute UPDATE query via @NamedQuery in active contaner managed transaction?
I would like to point out something first. What you refer is actually a JTA transaction. Now, JTA transaction can be handled by the container (which is called CMT) or by the bean (BMT).
What you are now doing is BMT (and hence the usage of UserTransaction instance).
In this case, you should either a) start the transaction even before you create the EntityManager (or) b) call methods on entityManager including createQuery after you start the transaction (if this doesn't work, I think calling entityManager.joinTransaction() should work before you call methods on the entityManager)