尝试使用 JPA 和 EJB 开发REST应用时,我遇到了下一个问题。
我在NetBeans中有3个项目(相同的工作区,只是项目,不是EAR或那些奇怪的东西):
其中一个是Java应用程序,我在其中从数据库创建了一些实体类。在同一个项目中,我创建了包含来自实体的控制器类的其他包。我测试了创建java主类并使用控制器的项目。
另一个项目是一个EJB模块,它就像一个包一样简单,并且是其中的下一个类:
package com.studentejb;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import studentdao.StudentTO;
import studentdao.StudentJpaController;
@Stateless
@LocalBean
public class StudentEJB {
private StudentJpaController studentDao = new StudentJpaController();
public List<StudentTO> findStudents() {
return studentDao.findStudentTO();
}
}
如您所见,我导入了之前的JAR项目(具有实体控制器的项目)。我还使用java主类测试了这个EJB项目,并且它可以工作。
最后一个项目是一个Web应用程序,您可以想象我想使用上一个项目的EJB模块,但在尝试输入资源http://localhost:8080/studentWeb/student/
时出现以下错误:
12:00:12,384 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /studentWeb/student/hola: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java:49)
.
.
.
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ExceptionInInitializerError
at studentdao.StudentJpaController.<init>(StudentJpaController.java:40)
at com.studentejb.StudentEJB.<init>(StudentEJB.java:23)
at com.studentweb.Resources.<init>(Resources.java:52)
at com.studentweb.Resources$Proxy$_$$_WeldClientProxy.<init>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
.
.
.
... 58 more
Caused by: javax.persistence.PersistenceException: Unable to locate persistence units
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:99)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at studentdao.EntityProvider.<clinit>(EntityProvider.java:20)
... 80 more
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-elt.1.a: Cannot find the declaration of element 'persistence'.
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:377)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:310)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:114)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:104)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:86)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:95)
... 85 more
以下是Web应用程序的代码(Resources.java):
@Path("/student")
public class Resources {
@EJB
private StudentEJB studentEjb;
@Context
private UriInfo context;
public Resources() throws FileNotFoundException, IOException, NamingException {
studentEjb = new StudentEJB();
}
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getJson() {
return Response.ok(studentEjb.findStudents()).build();
}
}
所以我使用2个JAR作为Web项目,一个使用EJB模块,另一个使用实体和控制器..
以下是 JPA 项目生成的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://xmlns.jcp.org/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="studentJPAPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
<class>studentjpa.Address</class>
<class>studentjpa.Phone</class>
<class>studentjpa.Student</class>
<class>studentjpa.Email</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=tca_student_db;integratedSecurity=true"/>
<property name="javax.persistence.jdbc.user" value="xxxx"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
</properties>
</persistence-unit>
</persistence>
谢谢!
答案 0 :(得分:1)
引起:javax.persistence.PersistenceException:无效 persistence.xml中。解析XML时出错[line:-1,column:-1]: cvc-elt.1.a:找不到元素声明&#39;持久性&#39;。
这里:
<persistence version="2.0" xmlns="http://xmlns.jcp.org/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">
xmlns:http://xmlns.jcp.org/xml/ns/persistence
与xsischemaLocation中的命名空间不匹配,即http://java.sun.com/xml/ns/persistence
尝试在两者中使用相同的内容:
<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">