13

Entity:

@Entity
@Table(name="user_account")
public class UserAccount implements Serializable{
    private static final long serialVersionUID = -2606506548742732094L;

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer id;

persistence.xml

<persistence-unit name="Maze" transaction-type="RESOURCE_LOCAL">

    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mazedb"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
    </properties>

</persistence-unit>

The error

WARNING: #{accountController.performLogin}: java.lang.IllegalArgumentException: No [EntityType] was found for the key class [com.maze.model.UserAccount] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>com.maze.model.UserAccount</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
javax.faces.FacesException: #{accountController.performLogin}: java.lang.IllegalArgumentException: No [EntityType] was found for the key class [com.maze.model.UserAccount] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>com.maze.model.UserAccount</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)

Behaviour Every time I restart the Web Container everything works just fine for a couple of minutes and than this error gets thrown. What am I missing?

EDIT :

Now I use an entity on the application scope as well and the same error occurs for the Country Entity. Both User and Country are defined as classes in the persistance.xml. Furthermore the exclude-unlisted-classes is set to false.

SEVERE: Critical error during deployment: 
java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at     sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39    )
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:256)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:255)
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:4750)
at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:550)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5366)
at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2000)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1651)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:294)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:462)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:382)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1064)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1244)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1232)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:459)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:209)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:238)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: No [EntityType] was found for the key class [com.maze.model.Country] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>com.maze.model.Country</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entityEmbeddableManagedTypeNotFound(MetamodelImpl.java:174)
at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entity(MetamodelImpl.java:194)
at org.eclipse.persistence.internal.jpa.querydef.AbstractQueryImpl.from(AbstractQueryImpl.java:97)
at com.maze.service.CountryService.readList(CountryService.java:74)
at com.maze.util.MazeConstants.<clinit>(MazeConstants.java:26)

The Country entity:

@Entity
@Table(name = "country")
public class Country implements Serializable{

private static final long serialVersionUID = 4086034429815316972L;

@Id
private Integer id;
private String name;
private String continent;
private Integer population;
private Float surface;

@OneToMany(mappedBy = "country")
private List<User> userList;

Now that this entity is used on application scope and the Entity throws this error the Glassfish fails to start completely.

SECOND EDIT : Based on the current behavior it seems that the web container starts and works fine until the first hot deployment.

TGM
  • 1,659
  • 10
  • 30
  • 45
  • May I bet that you're using Netbeans? Possibly related: http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_101:_20100218:_Descriptor.javaClass_is_null_on_a_container_EM_for_a_specific_case – BalusC Jan 09 '12 at 18:49
  • I'm using Eclipse Indigo Service Release 1 but that's the exact error I get. – TGM Jan 09 '12 at 19:10
  • I'm not sure. Did you try setting transaction type to JTA? That's also the most sensible choice in a Java EE web application (otherwise you've to manually write/copypaste a lot of ugly boilerplate code to begin, commit and rollback transactions). Your last stacktrace also tells that you're doing the JPA job inside a `static` initializer block. This should in theory work fine, but that's a bad idea in an EE environment. Do it in a (post)constructor of an application scoped bean instead. – BalusC Jan 09 '12 at 19:14
  • Currently I'm using Resource Local therefore I created a SingleTon Factory to manage my (static) EntityManager and EntityManager Factory. I'll try using a JTA. – TGM Jan 09 '12 at 19:18
  • Ah well.. You might not have done it correctly. Starters who use Singletons in a EE webapplication is often recipe for trouble. Don't forget to throw away those homegrown factories from your web app after switching to JTA. You don't need them at all. Just a `@Stateless` EJB with `@PersistenceContext` does the job more than sufficiently. – BalusC Jan 09 '12 at 19:19
  • One of the reasons I chose Resource Local in the first place was to avoid getting involved with EJB. – TGM Jan 09 '12 at 19:23
  • Oh? Perhaps you don't see its benefits or you have still its bad imago dated from the EJB 2.x era in mind? With EJB 3.x it's tremendously easy to create EJBs so that your container will take all the responsibility for managing transactions fully transparently. It's basically just a matter of putting the right annotations at the right places. – BalusC Jan 09 '12 at 19:25
  • Ok. I created a Connection on GlassFish (tested it with ping and it's all fine) changed the transaction settings. The web container starts but after the first hot deployment it throws the same error. (This is the same behavior as before) – TGM Jan 09 '12 at 19:51
  • In other words, you didn't throw away those homegrown factories nor switched to 100% EJBs? – BalusC Jan 11 '12 at 16:46
  • Yes. I'm a beginner with both JSF and JPA therefore I'm avoiding EJB for now. I updated my singleton factory and know every time the container is restarting or shutting down some methods are called to clean my environment. It seems like I managed to work around it but I know that's not probably a very healthy way of doing this stuff. – TGM Jan 11 '12 at 18:03
  • Avoiding? Why? *Properly* homegrowing a singleton entity manager factory and manually managing transactions is *much* harder for a beginner than just creating a javabean class on which all you need to do to get it to do its job done is to put the `@Stateless` annotation. As easy as you mark a javabean a JSF managed bean by putting the `@ManagedBean` annotation and a javabean a JPA entity by the `@Entity` annotation. This aversion against EJB makes no utter sense. Perhaps you incorrectly have had the -indeed terrible- EJB 2.x era form a decade ago in mind when thinking about EJBs. – BalusC Jan 11 '12 at 18:06
  • Thanks for your advice! I'll try it and I'll get back here if any problems occur. Thanks again! – TGM Jan 11 '12 at 18:13

6 Answers6

26

The problem is your hot deploying of your application. The old persistence unit is sticking around in the server because you never closed the old factory, so after the hot deploy the factory still has the old classes, so you get the class cast error.

You need to clear out the old persistence unit. Closing the EntityManagerFactory should do this, such as in a Servlet destroy callback.

If you use a managed persistence unit, then everything should get cleaned up automatically. The issue is in how you are managing your persistence unit.

I think there is also a bug for this logged on EclipseLink, you might want to vote for that bug.

James
  • 17,965
  • 11
  • 91
  • 146
5

I once encountered a similar problem. The exception was sometimes thrown, sometimes not. After a painful amount of time debugging I found out it was caused by a second persistence.xml file (containing a persistence unit with an identical name) included in another jar on the classpath.

jpo
  • 2,550
  • 2
  • 19
  • 16
1

I had a similar issue where I had refactored code, changing class names, and resource injection was still trying to inject the old class name. Renaming my persistence unit fixed the problem (and subsequently reverting the name reintroduced the problem).

Update: upon further searching, I came across a jar file for my Persistence classes. I had created it to work around a previous build problem. Compounding the issue, I had also renamed my projects. Though I had deleted the persistence JAR from my workspace, it was still hanging around eclipse's .xxx folders under the old project name. I deleted it and all references to the old project, and my build was fixed, getting rid of the No [EntityType] message.

brs5tettba
  • 124
  • 9
1

Can you add the classes manually to the persistence.xml? Ideally, you shouldn't have to do this, but it would at least give you a start.

<persistence-unit><class>com.maze.model.UserAccount</class>...

kcdragon
  • 1,724
  • 1
  • 14
  • 23
  • I tried that as well. Same result. Works fine for a while then it crashes. – TGM Jan 05 '12 at 19:43
  • You could try getting rid of the generated serial id. Maybe it's looking for a different version of the class? – kcdragon Jan 05 '12 at 19:51
  • Yes but how would you explain the fact that the application works for some time before failing. – TGM Jan 05 '12 at 19:58
  • Not sure, perhaps the server isn't finished loading something when you start viewing the static content? – kcdragon Jan 05 '12 at 20:02
  • Well, if the serialUID you are using in the class is different from the UID from a class stored in the Database could lead to this problem, no? – SHiRKiT Jan 09 '12 at 20:47
0

Can also occur due Unintentional Duplicate persistence files

Sometimes Netbeans can auto-generate a persistence file in your webModule, in case you had your persistence in a library module, it will be overridden by the new empty persistence that was autogenerated. Hence this error can also occur

electricalbah
  • 2,227
  • 2
  • 22
  • 36
-1

After some trial and error, following worked for me.

Assume module1 contains classes c1 and c2, module2 contains c3 and is dependent on module1. Our application contains class c5 and is dependent on module2.

Make sure module1 and module2 do not contain persistence.xml persistence.xml file in the main application should contain the following entries

    <class>com.domain.c1</class>
    <class>com.domain.c2</class>
    <class>com.domain.c3</class>
    <class>com.domain.c5</class>

Above entries are required, even if you have the below entry

    <exclude-unlisted-classes>
        false
    </exclude-unlisted-classes>

It solved all the below problems:

  1. List item
  2. Entities are sometimes recognized, sometimes not.
  3. Application runs fine in normal mode, but fails with error in eclipse debug mode.
  4. Application runs fine with the command mvn spring-boot:run, but fails with java -jar target\app.jar
Hulk
  • 6,399
  • 1
  • 30
  • 52
Jagadesh
  • 29
  • 1