Table of Contents
Problem Description
After redeploying a WebApplication using JPA you see the following error: 13:02:18.090 Unknown entity bean class: class com.hhu.wfjpa2el.Emp2, please verify that this class has been marked with the @Entity annotation. 13:02:18.091 java.lang.IllegalArgumentException: Unknown entity bean class: class com.hhu.wfjpa2el.Emp2, please verify that this class has been marked with the @Entity annotation.
Solution
- Restart your WEBServer or
- Install a ServletContextListener and close the EMF during contextDestroyed() Event
JAVA Code for implementing a WebListener
package com.hhu.wfjpa2el;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.LoggerFactory;
@WebListener
public class EMF implements ServletContextListener {
private static EntityManagerFactory emf;
final private static String pu = "jpaELPU";
final static org.slf4j.Logger logger = LoggerFactory.getLogger(ServletContextListenerImpl.class);
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("+++ ServletContextListener : contextInitialized - Inititalizing EMF for PU: " + pu);
emf = Persistence.createEntityManagerFactory(pu);
logger.info("+++ ServletContextListener : contextInitialized - Init EMF done for PU: " + pu);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
logger.info("+++ ServletContextListener : contextDestroyed - Closing EMF for PU: " + pu);
emf.close();
logger.info("+++ ServletContextListener : contextDestroyed - Closed EMF done for PU " + pu);
}
public static EntityManager createEntityManager() {
if (emf == null) {
throw new IllegalStateException("Context is not initialized yet.");
}
return emf.createEntityManager();
}
}
Initialize your Entity Manager by running : em = EMF.createEntityManager();
Java Code:
public static EntityManager getEntityManager()
{
EntityManager em = threadLocal.get();
if (em == null)
{
// setRunTimeInfo(getRunTimeInfo() + Tools.add_hmtl_pre_tag("Creating Entity Manager ! " ));
if ( enableLogger )
logger.info("Creating Entity Manager Factory ..." );
em = EMF.createEntityManager();
threadLocal.set(em);
}
return em;
}
Testing the Code wiht maven deploy/undeploy command
Deploy the WebApplication [oracle@wls1 WFJPA2EL]$ mvn wildfly:deploy Wilfdly Log report: 13:11:23,170 INFO [stdout] (MSC service thread 1-4) 13:11:23 [MSC service thread 1-4] INFO c.h.w.ServletContextListenerImpl - i +++ ServletContextListener : contextInitialized - Init EMF done for PU: jpaELPU 13:11:23,173 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-4) Initializing Mojarra 2.2.8-jbossorg-1 20140822-1131 for context '/WFJPA2EL-1.0' 13:11:23,518 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017534: Registered web context: /WFJPA2EL-1.0 13:11:24,010 INFO [org.jboss.as.server] (management-handler-thread - 1) JBAS018559: Deployed "WFJPA2EL-1.0.war" (runtime-name : "WFJPA2EL-1.0.war") Undeploy the WebApplication [oracle@wls1 WFJPA2EL]$ mvn wildfly:undeploy Wilfdly Log report: 13:11:23,170 INFO [stdout] (MSC service thread 1-4) 13:11:23 [MSC service thread 1-4] INFO c.h.w.ServletContextListenerImpl - i +++ ServletContextListener : contextInitialized - Init EMF done for PU: jpaELPU 13:11:23,173 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-4) Initializing Mojarra 2.2.8-jbossorg-1 20140822-1131 for context '/WFJPA2EL-1.0' 13:11:23,518 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017534: Registered web context: /WFJPA2EL-1.0 13:11:24,010 INFO [org.jboss.as.server] (management-handler-thread - 1) JBAS018559: Deployed "WFJPA2EL-1.0.war" (runtime-name : "WFJPA2EL-1.0.war") 13:13:02,751 INFO [org.wildfly.extension.undertow] (MSC service thread 1-7) JBAS017535: Unregistered web context: /WFJPA2EL-1.0
This was extremely helpful.
Thank you so much for taking the time to document this!
can’t find ServletContextListenerImpl