Search code examples
hibernatejpajakarta-eetransactionsejb

Wildfly JPA: How to ensure that an EJB query is in a Transaction


I want to execute an Update query from a Java Entity on Wildfly JEE. Here's how I update the Entity:

entry.setProcessed(Boolean.TRUE);
entry.setName("Fred");
em.persist(entry);

But it's not working, and it's giving me an error:

2016-12-09 17:23:23,226 ERROR [stderr] (default task-4) javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)

OK, so I need an Transaction. But how do I create or join the transaction?

Here's how I set up the PersistenceContext and EntityManager:

@PersistenceContext (unitName = "config", type=PersistenceContextType.EXTENDED)
private EntityManager em;

I have tried em.joinTransaction(), but this needs a Transaction that is already created. em.persist ()` doesn't work because there is no transaction. The Transaction is supposed to be managed by the container. I cant find a good reference as to how to set up the container, or configure the PersistenceContext.


Solution

  • Try to add annotation @Stateless on your class

    Example:

    @Stateless
    public class VehicleRepo {
    
    }