Java 如何将EntityManager正确注入JAX-WSWeb服务?
我正在尝试实现一个JAX-WSWeb服务,它有一个从配置的数据源返回数据的端点。我设法创建了服务和客户机,但我无法访问数据库,因为当我尝试使用注入的EntityManager时,会出现空指针异常。 这是我的服务:Java 如何将EntityManager正确注入JAX-WSWeb服务?,java,jpa,jax-ws,Java,Jpa,Jax Ws,我正在尝试实现一个JAX-WSWeb服务,它有一个从配置的数据源返回数据的端点。我设法创建了服务和客户机,但我无法访问数据库,因为当我尝试使用注入的EntityManager时,会出现空指针异常。 这是我的服务: @WebService(serviceName="ViewDocuments") public class ViewDocumentService { @PersistenceContext(unitName = "HibernateJPA&qu
@WebService(serviceName="ViewDocuments")
public class ViewDocumentService {
@PersistenceContext(unitName = "HibernateJPA")
private EntityManager em;
@WebMethod
public String sayHello() {
return "Hello world!";
}
@Produces("application/json")
@WebMethod(operationName="getDocuments")
public List<File> getDocuments(@WebParam(name = "name") String filename) {
if(filename == null)
return em.createQuery("FROM File").getResultList();
else return null;
}
}
我尝试在此处调用函数:
public class MainDocumentClient {
public static void main(String[] args) throws Exception {
URL wsdlUrl = new URL("http://localhost:8080/lab8/ViewDocuments?wsdl");
QName serviceName = new QName("http://webservices/", "ViewDocuments");
ViewDocumentClient service = new ViewDocumentClient(wsdlUrl, serviceName);
ViewDocumentService viewDocumentService = service.getDocumentPort();
System.out.println(viewDocumentService.sayHello());
System.out.println(viewDocumentService.getDocuments(null));
}
}
这是我的persistence.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="HibernateJPA" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/Postgres-Source8</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
<persistence-unit name="HibernateJPAForTests" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/lab8" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="<password>" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
</persistence>
org.hibernate.jpa.HibernatePersistenceProvider
java:/Postgres-Source8
假的
org.hibernate.jpa.HibernatePersistenceProvider
假的
sayHello()
方法工作正常。我还尝试使用EntityManager工厂
,手动实例化我的存储库
,添加@无状态
注释,但似乎没有任何效果。我就是不能查询我的数据库。
经过大量的研究,我似乎仍然无法理解如何正确地注入EntityManager。有人能给我解释一下吗
我使用的是Wildfly21.0.0。您是以war文件的形式部署到Wildfly的,对吗?是的,没错,然后您需要转到Wildfly子系统并在部署文件之前配置数据源。数据源可能没有在wildfly中定义以供使用。我已经这样做了,我需要persistence.xml文件的JNDI。我的项目成功地从托管bean执行CRUD操作。我唯一的问题是Web服务。请共享persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="HibernateJPA" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/Postgres-Source8</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
<persistence-unit name="HibernateJPAForTests" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/lab8" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="<password>" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
</persistence>