java 未知实体类错误消息,即使实体标有 @Entity 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5087611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:20:44  来源:igfitidea点击:

Unknown entity class error message even though the entity is marked with @Entity annotation

javarestjpaeclipselinktoplink

提问by Meow

I am building REST web app using Netbean6.9.1 and JPA EclipseLink.

我正在使用 Netbean6.9.1 和 JPA EclipseLink 构建 REST web 应用程序。

The issue I'm facing is even though my entity class MasatoTable is marked with Entity annotation, I get error:

我面临的问题是,即使我的实体类 MasatoTable 标有实体注释,我也收到错误消息:

    (java.lang.IllegalArgumentException: Unknown entity bean class:
     class entity.MasatoTable, please verify that this class
 has been marked with the @Entity annotation.)

The thing is when I restart the GlassFish3 server from NetbeanIDE, it works for a while and somehow at some point, the error start showing up.I used to use Toplink Essential and had no issue but I've changed to EclipseLink and re-defiend persistence.xml (shown below) and this problem started. But I don't see any code error that I can think of..

问题是当我从 NetbeanIDE 重新启动 GlassFish3 服务器时,它工作了一段时间,不知何故在某个时候,错误开始出现。我曾经使用过 Toplink Essential 并且没有任何问题,但我已经更改为 EclipseLink 并重新定义了 persistence.xml(如下所示)并且这个问题开始了。但我没有看到任何我能想到的代码错误..

MasatoTable.java

正人表.java

@Entity
@Table(name = "MasatoTable")
public class MasatoTable implements Serializable {
...continue code...

persistence.xml

持久化文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="kojoPU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>jdbc/koga_kojo</non-jta-data-source>
    <class>entity.MasatoTable</class>
    <properties>
      <property name="eclipselink.logging.logger" value="JavaLogger"/>
      <property name="eclipselink.logging.level.sql" value="FINEST"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://KOGADBSERVER;databaseName=MasatoDB"/>
      <property name="javax.persistence.jdbc.password" value="foobar"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.user" value="koga"/>
    </properties>
  </persistence-unit>
</persistence>

Looks like same issue but that ticket's solution is to rollback to Toplink from Eclipselink. Has anyone solve the issue without rolling back to toplink?

看起来像同样的问题,但该票的解决方案是从 Eclipselink 回滚到 Toplink。有没有人在不回滚到顶级链接的情况下解决了这个问题?

Unknown entity bean class after hot deploy: netbeans 6.9 + glassfish 2.1 + eclipselink jpa 2.0

热部署后的未知实体 bean 类:netbeans 6.9 + glassfish 2.1 + eclipselink jpa 2.0

UPDATE

更新

As far as my research goes in terms of this case, look like the bug has been addressed and fixed?

就我在这种情况下的研究而言,看起来该错误已得到解决和修复?

https://bugs.eclipse.org/bugs/show_bug.cgi?id=288141

https://bugs.eclipse.org/bugs/show_bug.cgi?id=288141

I'm not knowledgeable enough to understand the whole thing but there are few facts I found:

我的知识不够,无法理解整件事,但我发现的事实很少:

Netbean6.9.1 provides EclipseLink 2.0.1 whereas newest version is 2.2.0

Netbean6.9.1 提供 EclipseLink 2.0.1 而最新版本是 2.2.0

I downloaded newest version and put them into the lib dir then re-deployed but no luck, the issue still persists. Not sure if I did things in wrong way.

我下载了最新版本并将它们放入 lib 目录然后重新部署但没有运气,问题仍然存在。不确定我是否以错误的方式做事。

UPDATE after James's comment

詹姆斯发表评论后更新

Below is the class I use to create singleton EntityManagerFactory and how it is being used.

下面是我用来创建单例 EntityManagerFactory 的类以及它是如何使用的。

package common;

import java.util.Date;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EmProvider {

    private static final String DB_PU = "kojoPU"; //工場DB

    public static final boolean DEBUG = false;

    private static final EmProvider singleton = new EmProvider();

    private EntityManagerFactory emf;

    private EmProvider() {}

    public static EmProvider getInstance() {
        return singleton;
    }


    public EntityManagerFactory getEntityManagerFactory() {
        if(emf == null) {
            emf = Persistence.createEntityManagerFactory(DB_PU);
        }
        if(DEBUG) {
            System.out.println("factory created on: " + new Date());
        }
        return emf;
    }

    public void closeEmf() {
        if(emf.isOpen() || emf != null) {
            emf.close();
        }
        emf = null;
        if(DEBUG) {
            System.out.println("EMF closed at: " + new Date());
        }
    }

}//end class

How it is used:

如何使用:

EntityManager em = null;
   Object out = null;
   try {
       em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
       Query query = em.createNativeQuery(sql);

       ....some code here
   }
   finally {
       if(em != null) {
           em.close();
       }
   }

By looking at my code, I think... I'm not closing EntityManagerFactory(i'm closing EntityManager in finally though)

通过查看我的代码,我想......我没有关闭 EntityManagerFactory(虽然我最终关闭了 EntityManager)

I also I found the "unknown entity bean class"error in topic shows up for sure with following action.

我还发现"unknown entity bean class"主题中的错误肯定会出现在以下操作中。

  1. Deploy web app through Netbean (This will open the browser)

  2. Change some code in Netbean IDE and save it. Then Netbean will auto-re-deploy as soon as you press the save button (is this called "hot deploy" ???)

  3. Go back to browser and take some action like add some data which involves EntityManager.

  1. 通过 Netbean 部署 Web 应用程序(这将打开浏览器)

  2. 在 Netbean IDE 中更改一些代码并保存。然后,只要您按下保存按钮,Netbean 就会自动重新部署(这是否称为“热部署”???)

  3. 返回浏览器并执行一些操作,例如添加一些涉及 EntityManager 的数据。

Looks like the error occurs because I'm not closing entity manager factory when hot-deploy? I have to investigate.

看起来错误是因为我在热部署时没有关闭实体管理器工厂?我得去调查。

采纳答案by James

How are you creating your EntityManager/Factory, is it managed or non-managed?

您如何创建 EntityManager/Factory,它是托管的还是非托管的?

If it is not managed, then I believe that EclipseLink will not be notified in any way of a redeployment, so will still have the old EntityManagerFactory cached as it is never closed. Can you ensure that you close the EntityManagerFactory after the hot deploy?

如果它不受管理,那么我相信 EclipseLink 不会以任何方式收到重新部署的通知,因此仍然会缓存旧的 EntityManagerFactory,因为它永远不会关闭。你能确保在热部署后关闭 EntityManagerFactory 吗?

You can make the persistence unit a managed persistence unit, that may resolve the issue.

您可以将持久性单元设为托管持久性单元,这可能会解决问题。

Either way, please log a bug for this with details in EclipseLink and Glassfish, as this issue should be tracked.

无论哪种方式,请在 EclipseLink 和 Glassfish 中为此问题记录一个错误,因为应该跟踪这个问题。

I think this may be the bug, https://bugs.eclipse.org/bugs/show_bug.cgi?id=326552

我认为这可能是错误,https://bugs.eclipse.org/bugs/show_bug.cgi?id=326552

Please vote for this bug, and include your information in it.

请为此错误投票,并在其中包含您的信息。

回答by David O'Meara

The solution hereis to use JPA 1.0 if 2.0 is not required.

如果不需要 2.0,这里的解决方案是使用 JPA 1.0。

回答by VolatilitySmile

Today i faced the same proble, but i corrected in persistence.xml

今天我遇到了同样的问题,但我在persistence.xml中更正了

  1. Please check with entity class name and package are correctly defined in persistence.xml.
  1. 请检查实体类名称和包是否在persistence.xml 中正确定义。