How to use JPA from a JBoss Web application ?

In simple terms, if you are not using EJB or CDI, the user is expected to interact with javax.transaction.UserTransaction which defines the transaction boundaries.

Here is an example Servlet which persists an Entity by injecting the UserTransaction and adding the EntityManager via the @PersistenceContext annotation:

@WebServlet(name="JPAServlet", urlPatterns={"/jpademo"})
public class JPAServlet extends HttpServlet {
    private static final Logger LOG = Logger.getLogger(TestServlet.class);

    @Inject
    private UserTransaction txn;

    @PersistenceContext
    private EntityManager em;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            txn.begin();

            em.persist(new Customer(1, "John Smith"));

            txn.commit();
          } catch (NotSupportedException beginExceptions) {
              LOG.errorf(beginExceptions, "Cannot start transaction: '%s'", txn);
          } catch (SecurityException | IllegalStateException | RollbackException
                  | HeuristicMixedException | HeuristicRollbackException commitExceptions) {
              LOG.errorf(commitExceptions, "Cannot commit transaction: '%s'", txn);
          } catch (SystemException systemException) {
              LOG.errorf(systemException, "Unexpected error condition on work with transaction: '%s'", txn);
          }
    }
}

In the above example, the transaction-scoped @PersistenceContext coordinates with the (active) JTA transaction. When the transaction commits, the persistence context is flushed to the datasource (entity objects are detached but may still be referenced by application code). All entity changes that are expected to be saved to the datasource, must be made during a transaction.

Aside from that, you will only need, as usual a persistence.xml to provide a valid Database connection:

<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="primary">
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

The above example, uses the default Datasource available in WildFly, which is bound to “java:jboss/datasources/ExampleDS”