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

Reply via email to