无法从 Web 应用程序使用 EJB

Can't use EJB from Web Application

我在尝试使用 JPAEJB 开发 REST 应用程序时遇到下一个问题。

我在 NetBeans 中有 3 个项目(相同的工作区,只是项目,而不是 EAR 或其他奇怪的东西):

如您所见,我导入了之前的 JAR 项目(带有实体控制器的项目)。我还使用 java main class 测试了这个 EJB 项目并且它有效。

这是网络应用程序 (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();
    }
}

所以我为 Web 项目使用了 2 个 JAR,一个带有 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>

谢谢!

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'.

这里:

<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">