JPA - 未知的实体bean类

时间:2008-10-01 20:07:44

标签: java netbeans jpa glassfish

希望我能正确解释这个问题。我有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;
...
}

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

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

我的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>

我收到此错误:

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)

我正在使用随附的glassfish版本运行netbeans 6.1。 MySql 5.0。

5 个答案:

答案 0 :(得分:5)

在persistence.xml

中的class标签中定义此实体

答案 1 :(得分:1)

根据错误消息以及我从您的代码中得到的结果,错误似乎出现在persistence.xml文件中,您是否可能更冗长?

答案 2 :(得分:0)

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

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

可在此处找到使用的说明:http://docs.oracle.com/cd/E19798-01/821-1752/beade/index.html

答案 3 :(得分:0)

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

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

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

我还创建了一个名为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);
        }
    }

DAO内部:

...

@Autowired
private EntityManagerFactory entityManagerFactory;

...

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

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

答案 4 :(得分:0)

Mario在这里提到 EntityManagerFactory 时是对的。

两者:

java.lang.IllegalArgumentException :未知的实体Bean类...

java.lang.IllegalStateException :此Web容器尚未启动...

当您多次重新部署Web应用程序但未正确关闭 EntityManagerFactory 时,会发生这些异常。

跟随this instruction注册 ServletContextListener ,并跟随this instruction正确关闭 EntityManagerFactory