简体   繁体   中英

JPA - Unknown entity bean class

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

@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

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

My persistence.xml looks like this:

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

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

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

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.

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.

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

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

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:

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:

...

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

Mario was right when he mentions EntityManagerFactory here.

Both:

java.lang.IllegalArgumentException : Unknown entity bean class...

and

java.lang.IllegalStateException : This web container has not yet been started...

These exceptions occur when you redeploy a web application multiple times but didn't close EntityManagerFactory properly.

follow this instruction to register ServletContextListener and this instruction to close EntityManagerFactory properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM