[英]Can't use EJB from Web Application
I have the next problem while trying to develope a REST app using JPA and EJB 's. 在尝试使用JPA和EJB开发REST应用程序时遇到下一个问题。
I have 3 projects in NetBeans (same workspace, just projects, not EAR or that weird stuff): 我在NetBeans中有3个项目(相同的工作空间,只是项目,而不是EAR或那怪异的东西):
One of them is a Java Application in which I created some entity classes from a database. 其中之一是Java应用程序,我在其中从数据库中创建了一些实体类。 Inside of the same project, I created other package containing the controller classes from the entities.
在同一个项目中,我创建了另一个包含来自实体的控制器类的包。 I tested the project creating a java main class and using the controllers.
我测试了创建Java主类并使用控制器的项目。
The other project is a EJB module, it as simple as a package and the next class inside of it: 另一个项目是一个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(); } }
As you can see, I imported the previous JAR project (the one with the entity's controllers). 如您所见,我导入了先前的JAR项目(带有实体控制器的项目)。 I also tested this EJB project with a java main class and it worked.
我还使用Java主类测试了该EJB项目,并且该项目有效。
The last project is a web application, as you can imagine I want to use the EJB modules of the previous project but I get the following errors while trying to enter to the resource http://localhost:8080/studentWeb/student/
: 正如您可以想象的那样,最后一个项目是一个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
Here is the code of the web app (Resources.java): 这是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();
}
}
So I am using 2 JARs for the web project, the one with the EJB modules and the other with the entities and controllers.. 因此,我在Web项目中使用2个JAR,一个JAR与EJB模块,另一个与实体和控制器。
Here is the persistence.xml generated by the JPA project: 这是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>
Thank you! 谢谢!
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
原因:javax.persistence.PersistenceException:无效的persistence.xml。 Error parsing XML [line : -1, column : -1] : cvc-elt.1.a: Cannot find the declaration of element 'persistence'.
解析XML时出错[行:-1,列:-1]:cvc-elt.1.a:找不到元素“持久性”的声明。
Here : 这里 :
<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
doesn't match with the namespace in xsischemaLocation which is http://java.sun.com/xml/ns/persistence
xmlns:
http://xmlns.jcp.org/xml/ns/persistence
://xmlns.jcp.org/xml/ns/persistence与xsischemaLocation中的命名空间不匹配,该位置是http://java.sun.com/xml/ns/persistence
Try to use the same in both : 尝试在两者中使用相同的内容:
<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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.