How to correctly inject EntityManager into JAX-WS web service?

Viewed 196

I'm trying to implement a JAX-WS web service which has an endpoint that returns data from the configured data source. I managed to create the service and the client, but I can't access the database because I get Null Pointer Exceptions when I try to use the injected EntityManager. Here is my service:

@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;
    }
}

And here is my WebServiceClient:

@WebServiceClient(name="ViewDocuments", wsdlLocation = "http://localhost:8080/lab8/ViewDocuments?wsdl")
public class ViewDocumentClient extends Service {

    protected ViewDocumentClient(URL wsdlDocumentLocation, QName serviceName) {
        super(wsdlDocumentLocation, serviceName);
    }

    @WebEndpoint(name = "DocumentPort")
    public ViewDocumentService getDocumentPort() {
        return new ViewDocumentService();
    }
}

I am trying to call the function here:

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));
    }
}

This is my persistence.xml file content:

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

The sayHello() method works fine. I also tried to use an EntityManagerFactory, to instantiate my Repository by hand, to add the @Stateless annotation, but nothing seems to work. I just can't query my database. After lots of research, I still can't seem to understand how to properly inject the EntityManager. Can someone explain this to me, please?

I am using Wildfly21.0.0.

0 Answers
Related