我正在使用NetBeans创建我的第一个JPA应用程序。我无法使持久性工作。与数据库的连接运行良好,当我运行应用程序时,创建了数据库表。但是当我尝试创建EntityManagerFactory时:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PISProjektPU");
我明白了:
INFO: javax.persistence.PersistenceException: [PersistenceUnit: PISProjektPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at cz.vutbr.fit.pis.spravaTechniky.service.TestManager.<init>(TestManager.java:28)
at cz.vutbr.fit.pis.spravaTechniky.service.__EJB31_Generated__TestManager__Intf____Bean__.<init>(Unknown Source)
...
我的persistence.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="PISProjektPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myslq_spravaTechniky</jta-data-source>
<class>cz.vutbr.fit.pis.spravaTechniky.data.TestEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
它就在这里:
src/conf/persistence.xml
build/web/WEB-INF/classes/META-INF/persistence.xml
我搜索了论坛并找到了一些提示如何删除此错误,但我无法使其正常工作。我试着将这两行添加到Manifest.mf:
Meta-Persistence: META-INF/persistence.xml
JPA-PersistenceUnits: PISProjektPU
我尝试将persistence.xml文件移动到所有可能的位置。我还添加了所有看起来可能有用的库,当我转到Properties / Libraries时,我看到了:
Java EE 6 API Library
Hibernate
Java-EE-GlassFish-v3
EclipseLink(JPA 2.0)
EclipseLink-GlassFish-v3
Hibernate JPA
JSF 2.0
Java EE Web 6 API Library
Persistence
我确定我做了一些愚蠢的简单错误,但经过一天试图完成这项工作后,我无法看出问题出在哪里。说实话,我现在完全混淆了放置哪个文件或如何配置所有内容,所以我随机尝试不同的东西。我会感谢任何建议!
感谢您的建议。我的测试类实际上看起来像这样:
Class TestManager:
@Stateless
public class TestManager {
@PersistenceContext
private EntityManager em;
public void save(TestEntity t) {
em.merge(t);
}
public void remove(TestEntity t) {
em.remove(em.merge(t));
}
public void create(TestEntity t) {
em.persist(t);
}
@SuppressWarnings("unchecked")
public List<TestEntity> findAll() {
return em.createQuery("SELECT t FROM TestEntity t").getResultList();
}
}
Class TestBean:
@Named(value="testBean")
@Dependent
public class TestBean {
@EJB
private TestManager testManager;
/** Creates a new instance of TestBean */
public TestBean() {
}
public List<TestEntity> getEntities() {
return this.testManager.findAll();
}
}
我正在调用TestBean.getEntities方法:
...
<h:dataTable value="#{testBean.entities}" var="entity">
...
这会导致以下异常:
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:84)
at $Proxy141.findAll(Unknown Source)
at cz.vutbr.fit.pis.spravaTechniky.service.__EJB31_Generated__TestManager__Intf____Bean__.findAll(Unknown Source)
at cz.vutbr.fit.pis.spravaTechniky.back.TestBean.getEntities(TestBean.java:27)
...
我尝试用@EJB替换@PersistenceContext,但得到javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
。
答案 0 :(得分:0)
尝试使用这个
@EJB
EntityManager em;
em.persist(someobject);
而不是工厂,如果你需要使用工厂,我建议你重复设置IDE中实体的持久性的步骤
答案 1 :(得分:0)
要纠正的几件事情:
@EJB
注释的@Inject
注释。使用你正在使用的堆栈应该可以工作。@Dependent
注释)与根本不建立范围(没有注释)相同。从bean类中删除@Dependent
注释,并在其中放置@RequestScoped
(@SessionScoped
,@ApplicationScoped
或您的bean应具有的任何范围。答案 2 :(得分:0)
最后,我使用NetBeans中的一个示例项目(File&gt; New Project&gt; Samples&gt; Java Web&gt; JSF JPA)使JPA工作。我用它作为我项目的基础。我不确定我做错了什么,但至少一切都工作正常。无论如何,谢谢你的帮助!