这些是错误中涉及的类(我不使用Entreprise Java Bean):
@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;
这是使用实体管理器的类。
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;
}
}
所以,例外是:JTA EntityManager不能使用getTransaction()
答案 0 :(得分:0)
最后,我只是在persistence.xml文件的源代码中将'JTA'替换为'RESOURCE_LOCAL':