2013-09-27 31 views
0

我在java ee 6框架中有一個J2EE項目。使用EJB和War模塊。 我想添加一個服務到EJB模塊來處理數據庫中的一些數據,所以我需要訪問服務方法中的DAO方法。但它返回錯誤!如何在java ee 6 Framework中將數據保存在JAX-WS Web服務中?

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

EJB類代碼爲:

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

測試服務使這個錯誤在服務器上:

 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) 
+0

你可以在UserEJB類中添加addUser方法的代碼。 NPE來自UserEJB的addUser方法。 –

+0

問題是em在userEjb.addUser()中爲null我不知道爲什麼? –

回答

1

由於您自己創建了UserEJB對象,您的EntityManger不會被容器注入!你已經打破了注射鏈。使用DI時,您必須讓容器爲您創建對象。試試這個方法:

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

是的,你是對的。但它也不起作用。同樣的錯誤。我不知道如何告訴容器爲這個類創建對象。 –

+0

另一個問題!如何在服務測試中調試服務(設置中斷點,..)? –

+0

TANX ....它確實工作!我犯了這樣一個錯誤。 –

0

該錯誤是內部UserEJB.java類在管線34 ,所以請張貼該代碼。

+0

添加了代碼行34:em.persist(user) –

相關問題