Issue
These are the classes implicated in the error (I'm not Using The Entreprise Java Beans):
@WebListener
public class EMF implements ServletContextListener {
private static EntityManagerFactory emf;
private final String PERSISTENCE_UNIT_NAME = "ldnr";
@Override
public void contextInitialized(ServletContextEvent event) {
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.user", "root");
properties.put("javax.persistence.jdbc.password", "1234512345");
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
emf.close();
}
public static EntityManager createEntityManager() {
if (emf == null) {
throw new IllegalStateException("Context is not initialized yet.");
}
return emf.createEntityManager();
}
}
package fr.ldnr.daoImpl;
This is the class using the entity manager.
public abstract class DAOFactoryImpl implements DAOFactory {
private final EntityManager em;
public DAOFactoryImpl() {
em = EMF.createEntityManager();
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save(Object o) throws Exception {
em.getTransaction().begin();
em.persist(o);
em.flush();
em.clear();
em.close();
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Object update(Object o) throws Exception {
o = em.merge(o);
em.flush();
em.clear();
em.close();
return o;
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Object find(Class myClass, long id) throws Exception {
Object o = em.find(myClass, id);
em.clear();
em.close();
return o;
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public List<Object> findAll(Class myClass) throws Exception {
List<Object> objectsList = em.createQuery("select * from " + myClass.getClass().getSimpleName()).getResultList();
return objectsList;
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete(Object o) throws Exception {
em.remove(o);
em.flush();
em.clear();
em.close();
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Object queryObject(String query) throws Exception {
Object o = em.createQuery(query).getSingleResult();
em.flush();
em.clear();
em.close();
return o;
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public List<Object> queryObjects(String query) throws Exception {
List<Object> objectsList = em.createQuery(query).getResultList();
em.flush();
em.clear();
em.close();
return objectsList;
}
@Override
@Transactional
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public long count(Class entityClass) throws Exception {
Object o = em.createQuery("select count(*) from " + entityClass.getSimpleName());
em.clear();
em.close();
return (Long) o;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
return properties;
}
}
So, the exception is: A JTA EntityManager cannot use getTransaction()
Solution
Finally I simply replaced 'JTA' with 'RESOURCE_LOCAL' in this source code in the persistence.xml file:
Answered By - Patrick Dapa
Answer Checked By - Willingham (JavaFixing Volunteer)