这是我对Entity bean的第一次尝试,我反复收到以下错误:
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
我使用Netbeans 7.3和GlassFish 3.1.2.2 Server编写了一个简单的实体bean示例。我的persistance.xml文件似乎有问题。但是,我无法解决这个问题。我读了
JavaEE 6: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
Unable to retrieve EntityManagerFactory for unitName null for simple EJB - 请参阅nosferatum回答
但是几小时后我也无法解决这个问题。我附上了我的目录结构的屏幕截图以及我的XML文件的代码,希望有人可以指出错误。
persistance.xml :(由NetBeans自动生成的代码)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="EnterpriseApplication3-ejbPU" transaction-type="JTA">
<jta-data-source>TestDatabase</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
代码失败:
宣言:
// This injects the default entity manager factory
@PersistenceUnit
private EntityManagerFactory emf;
调用点:
EntityManager em = emf.createEntityManager();
此外,有些人似乎这样做:
@PersistenceContext(unitName = "myPU")
并且在persistance.xml中具有相同的名称。我快速搜索了所有项目文件,但未提供@PersistenceContext注释。但我补充说
@PersistenceContext(unitName = "EnterpriseApplication3-ejbPU")
到我调用EntityManagerFactory的代码。但仍然没有成功:(
答案 0 :(得分:0)
试试这个:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EnterpriseApplication3-ejbPU");
EntityManager em = emf.createEntityManager();