saeed shokoohi
saeed shokoohi

Reputation: 17

How to persist data in JAX-WS web services in java ee 6 Framework?

I have an J2EE project in java ee 6 framework. with EJB and War module. I want to add a Service to EJB module to manipulate some data in data base , so i need to access the DAO methods in Service methods . but it return error!

@WebService(serviceName = "AddUserService")
@Stateless()
public class AddUserService {
   @PersistenceContext(unitName = "UserRegistrationService-ejbPU")
    EntityManager em;

    @WebMethod(operationName = "registerUser")
    public String registerUser(BasicUserInfo basicUserInfo) {
        UserEJB userEJB=new UserEJB(em);
        String retValue = "";
        User user = new User();
        user.setAge(basicUserInfo.getAge());
        user.setUserName(userName);
        userEJB.addUser(user);
        retValue = " User Registered with user_ID:" + user.getId().toString() + "  and userName:" + user.getUserName();
        return retValue;
    }
}

the EJB class codes are :

    @TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless
public class UserEJB {

    @PersistenceContext(unitName = "UserRegistrationService-ejbPU")
    EntityManager em;

    public UserEJB() {
    }

    public UserEJB(EntityManager em) {
        this.em = em;
    }

    public void addUser(User user) {
        em.persist(user);
    }
}

testing the service make this error on the server:

        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
    at dao.UserEJB.addUser(UserEJB.java:34)
    at app.Services.AddUserService.registerUser2(AddUserService.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)

Upvotes: 0

Views: 667

Answers (2)

Yngwie89
Yngwie89

Reputation: 1217

Your EntityManger doesn't get injected by the container because you've create the UserEJB object by yourself! You've broken the injection chain. When using DI you must let the container create the object for you. Try this way:

@WebService(serviceName = "AddUserService")
@Stateless()
public class AddUserService {

    //actually you don't need this here, you'll get that injected inside the UserEJB
    @PersistenceContext(unitName = "UserRegistrationService-ejbPU")
    EntityManager em;


    @Inject
    UserEJB userEjb;

    @WebMethod(operationName = "registerUser")
    public String registerUser(BasicUserInfo basicUserInfo) {
        //you don't need this line, the container will instantiate the object for you!
        //UserEJB userEJB=new UserEJB(em);
        String retValue = "";
        User user = new User();
        user.setAge(basicUserInfo.getAge());
        user.setUserName(userName);
        userEJB.addUser(user);
        retValue = " User Registered with user_ID:" + user.getId().toString() + "  and         userName:" + user.getUserName();
        return retValue;
    }
}

Upvotes: 1

Yngwie89
Yngwie89

Reputation: 1217

The error is inside class UserEJB.java at line 34, so please post that code.

Upvotes: 0

Related Questions