java.lang.illegalstateexception无法为unitname null检索entitymanagerfactory

时间:2015-02-12 17:54:01

标签: java maven java-ee jpa glassfish

我写了Entity.jar。它的EJB模块包含实体类和etitydao类。当我在GlassFish上运行它时,我有错误:

Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:158)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
    at com.styl.thincapayment.dao.impl.DataAccessImpl.insertToDatabase(DataAccessImpl.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

我认为它来自persistence.xml。我说错了地方。如何解决此问题。

  • 我将持久性文件放在entities / src / resources / persistence.xml中。我使用Maven来管理项目

  • 声明EntityManager:

    @PersistenceContext(的unitName ="实体&#34)

    受保护的EntityManager entityManager;

1 个答案:

答案 0 :(得分:1)

persistence.xml应放在文件夹CLASS_PATH_ROOT/META-INF中。

如果您使用的是Maven的standard project layout,则路径应为src/main/resources/META-INF/persistence.xml