No [EntityType] was found for the key class […] in the Metamodel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 11:10:42

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.

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.

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>...

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.

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

Jagadesh

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!