我编写了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 。我把它放错地方了。如何解决这个问题。
我将持久性文件放在Entity/src/resources/persistence.xml中。我使用Maven来管理项目
声明EntityManager:
@PersistenceContext(unitName="entities")
protected EntityManager实体管理器;
最佳答案
persistence.xml
应该放在文件夹CLASS_PATH_ROOT/META-INF
内。
如果你使用的是Maven的standard project layout ,路径应该是src/main/resources/META-INF/persistence.xml
关于java.lang.illegalstateException无法检索entitymanagerfactory的unitname null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484434/