Hi do you mix query usage outside and inside a tx? I think the wiser is to choose one of both but not mixing it to avoid issues.
Romain Manni-Bucau @rmannibucau <https://twitter.com/rmannibucau> | Blog <https://blog-rmannibucau.rhcloud.com> | Old Wordpress Blog <http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> | LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber <http://www.tomitribe.com> | JavaEE Factory <https://javaeefactory-rmannibucau.rhcloud.com> 2016-07-12 16:28 GMT+02:00 Felipe Jaekel <[email protected]>: > Hi, > > I have the following method on my JPA base DAO: > > protected int executeUpdate(final Query... queries) > { > int total = 0; > > try > { > beginTransaction(); > > for(final Query query : queries) > { > total += query.executeUpdate(); > > if(isLoggable()) > { > LogService.logUpdate(em, query, getPersistentClass().getSimpleName()); > } > } > > commitTransaction(); > } > catch(final Exception e) > { > rollbackTransaction(e); > } > > return total; > } > > After I changed it from CDI produced EntityManager to JTA + bean managed > transactions, Hibernate complains that the query parameters are not set. > > I noticed that it is because I'm creating the query before the transaction > was started, and *JtaQuery.getEntityManager()* is like this: > > private EntityManager getEntityManager() { > if (!underTx) { > entityManager = jtaEntityManager.getEntityManager(); > this.underTx = jtaEntityManager.isTransactionActive(); > createQuery(); > } > return entityManager; > } > > Since *underTx* is updated to *true* before *createQuery() *is called, the > query parameters are not bound again: > > private Query createQuery() { > if (!unwrap) { > query = jtaEntityManager.createQuery(queryType(), > entityManager, method, args); > } > if (!underTx) { > for (final QueryOperation op : appliedOperations) { > query = op.apply(query); > } > } > return query; > } > > TomEE version is 7.0.1. > > I'd like to know if you could change *getEntityManager()* to update > *underTx* after *createQuery()* is called, or if I shouldn't create queries > outside transactions at all when using JTA. > > Thanks >
