Java JPA - 未知实体 bean 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/159469/
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-08-11 09:53:37  来源:igfitidea点击:

JPA - Unknown entity bean class

javanetbeansjpaglassfish

提问by arinte

Hopefully, I can explain this issue properly. I have 3 classes that deals with my entities.

希望我能正确解释这个问题。我有 3 个处理我的实体的类。

@MappedSuperclass
public abstract class Swab implements ISwab {
...
    private Collection<SwabAccounts> accounts;
...
}

@Entity
@Table(name="switches")
@DiscriminatorColumn(name="type")
@DiscriminatorValue(value="DMS500")
public class DmsSwab extends Swab implements ISwab, Serializable {
...
    private ObjectPool pool;
...
    @Transient 
    public ObjectPool getPool(){
        return pool;
    }
...
}

@Entity(name="swab_accounts")
public class SwabAccounts implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int swab_account_id;
    private int swab_id;
...
}

And in a EJB a query is being doing this way

并且在 EJB 中,查询正在以这种方式进行

    DmsSwab dms = em.find(DmsSwab.class, 2);
    List<Swab> s = new ArrayList<Swab>(1);
    s.add(dms);

My persistence.xml looks like this:

我的persistence.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="dflow-pu" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>com.dcom.sap.dms.DmsSwab</class>
    <class>com.dcom.sap.jpa.SwabAccounts</class>
    <properties>
      <property name="toplink.jdbc.user" value="dflow"/>
      <property name="toplink.jdbc.password" value="dflow"/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://itcd-400447:3306/dflow"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
  </persistence-unit>
</persistence>

I get this error:

我收到此错误:

java.lang.IllegalArgumentException: Unknown entity bean class: class com.dcom.sap.dms.DmsSwab, please verify that this class has been marked with the @Entity annotation.
com.dcom.sap.SwabException: java.lang.IllegalArgumentException: Unknown entity bean class: class com.dcom.sap.dms.DmsSwab, please verify that this class has been marked with the @Entity annotation.
Caused by: java.lang.IllegalArgumentException: Unknown entity bean class: class com.dcom.sap.dms.DmsSwab, please verify that this class has been marked with the @Entity annotation.
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.findInternal(EntityManagerImpl.java:306)
        at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.find(EntityManagerImpl.java:148)

I am running netbeans 6.1 with the version of glassfish that comes with it. MySql 5.0.

我正在运行 netbeans 6.1 及其附带的 glassfish 版本。mysql 5.0。

回答by Nicolas

According to the error message and what I figure from your code, the error seems to be in the persistence.xml file, can you be a bit more verbose ?

根据错误消息和我从您的代码中得出的结论,错误似乎在persistence.xml 文件中,您能说得更详细一点吗?

回答by Andy

define this entity in class tag inside the persistence.xml

在persistence.xml中的class标签中定义这个实体

回答by clebertx

I had the same error and, complementing the information above, my case was a ClassLoader issue. My app has three files. A ejb-module.jar which depends on app-lib.jar (library that contains pojo and database entities) and a web-module.war which depends on app-lib.jar.

我有同样的错误,补充上面的信息,我的情况是一个类加载器问题。我的应用程序有三个文件。一个 ejb-module.jar 依赖于 app-lib.jar(包含 pojo 和数据库实体的库)和一个 web-module.war 依赖于 app-lib.jar。

In the deployment, the app-lib.jar was loaded twice by the glassfish. Googling, I found out that I should copy the app-lib.jar to a "shared" lib in the glassfish domain. I've copied the postgresql.jar to "domain-dir/lib" and my app-lib.jar to "domain-dir/lib/applibs". Have it done, the app worked like a charm.

在部署中,app-lib.jar 被 glassfish 加载了两次。谷歌搜索,我发现我应该将 app-lib.jar 复制到 glassfish 域中的“共享”库。我已将 postgresql.jar 复制到“domain-dir/lib”并将我的 app-lib.jar 复制到“domain-dir/lib/applibs”。完成后,该应用程序就像一个魅力。

The used explanation can be found here: http://docs.oracle.com/cd/E19798-01/821-1752/beade/index.html

使用的解释可以在这里找到:http: //docs.oracle.com/cd/E19798-01/821-1752/beade/index.html

回答by Mario Barreto MX

I solved this issue creating a ContextListener in to my Web App, invoking the close of the entity manager factory at destroy context, :

我解决了这个问题,在我的 Web 应用程序中创建了一个 ContextListener,在销毁上下文时调用实体管理器工厂的关闭,:

public void contextDestroyed(ServletContextEvent servletContextEvent) {
    try {
        logger.info("contextDestroyed...");
        LifeCycleManager lifeCycleManager = ServiceLocator.getLifeCycleManager();
        lifeCycleManager.closeEntityManagerFactory();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

I also create a bean with name LifeCycleManager and inside them invoke a DAO method to close the entity manager factory:

我还创建了一个名为 LifeCycleManager 的 bean,并在其中调用 DAO 方法来关闭实体管理器工厂:

public void closeEntityManagerFactory() throws BusinessException {
        logger.info("closeEntityManager");
        try {
            logger.info("closing entity manager factory...");
            genericDAO.closeEntityManagerFactory();
            logger.info("Entity manager factiry closed");
        } catch (Exception e) {
            throw new BusinessException(BusinessErrorCode.CODIGO_EJEMPLO_01, Severity.ERROR);
        }
    }

Inside the DAO:

DAO 内部:

...

...

@Autowired
private EntityManagerFactory entityManagerFactory;

...

...

public void closeEntityManagerFactory() {
        logger.info("closing entity manager factory");
        getEntityManagerFactory().close();
        logger.info("entity manager factory closed");   
    }

Using this each time I deploy a change from my eclipse environment the destroy context is invoked. I hope could help you guys, my environment is WebLogic Server 11gR1 and JPA 1.0.

每次我从我的 Eclipse 环境部署更改时使用它,调用 destroy 上下文。我希望可以帮助你们,我的环境是 WebLogic Server 11gR1 和 JPA 1.0。