2013-03-18 61 views
0
my sessionfacade class 

     package com.entity; 

     import javax.ejb.Stateless; 
     import javax.persistence.EntityManager; 
     import javax.persistence.PersistenceContext; 

     @Stateless 
     public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeLocal 
     { 
     @PersistenceContext(unitName = "My_communityPU") 
     private EntityManager em; 

     @Override 
     protected EntityManager getEntityManager() { 
     return em; 
     } 

     public UsersFacade() { 
     super(Users.class); 
     } 

     } 



my managed bean class 

     package com.jsf; 

     import com.entity.Users; 
     import com.entity.UsersFacadeLocal; 
     import javax.inject.Named; 
     import javax.enterprise.context.SessionScoped; 
     import java.io.Serializable; 
     import java.util.List; 
     import java.util.Map; 
     import javax.annotation.ManagedBean; 
     import javax.ejb.EJB; 
     import javax.faces.context.ExternalContext; 
     import javax.faces.context.FacesContext; 

     @Named(value = "loginMB") 
     @ManagedBean 
     @SessionScoped 
     public class LoginMB implements Serializable { 
     @EJB 
     private UsersFacadeLocal usersFacade; 



     protected Users user; 
     protected List<Users> lusers; 
     protected String username;  
     protected String password; 

      public LoginMB() { 

     lusers=usersFacade.findAll(); 


      } 
      } 

我不知道爲什麼我的ejb注入到mangedbean不起作用。當我調用findall()時,我得到空指針異常。方法通過使用usersFacade 我正在與glassfish服務器的netbeans ide上工作。我只是在jsf中學習jpa,請讓我知道我在哪裏做錯了我越來越nullpointer例外,而我的管理bean中使用會話ejb

回答

6

容器僅在實例化託管bean後才注入EJB。使用@PostConstruct註釋並在那裏使用EJB。註釋後的方法將被調用。

+0

您的回答是否正確,謹慎展示一個示例以充分幫助OP? – 2013-03-18 14:27:39

+0

這還真管用謝謝阿卜杜拉·謝赫 – krishna 2013-03-18 16:28:19

+0

你好Luggi門多薩這裏是我工作圍繞@PostConstruct 公共無效的init(){ 的例子,如果(this.usersFacade.findAll()!= NULL){ this.lusers =這.usersFacade.findAll(); } } – krishna 2013-03-18 16:29:12