title: Get SessionScoped Bean's field through FacesContext
body: <p>I have the following classes:</p>
  
  <p>ClassA</p>
  
  <pre><code>import java.io.Serializable;
  
  import javax.enterprise.context.SessionScoped;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.persistence.EntityManager;
  
  import timereport.db.UserBean;
  import timereport.utils.JPAUtil;
  
  @Named("classA")
  @SessionScoped
  public class ClassA implements Serializable {
  
      @Inject
      protected UserBean userBean;
  
      public void logout() {
          userBean = null;
      }
  
      public void login() {
          EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
          userBean = em.find(UserBean.class, userBean.getUsername());
      }
  
      //setter and getter for userBean
  }
  </code></pre>
  
  <p>Here in <code>login()</code> I am doing </p>
  
  <pre><code>EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
  userBean = em.find(UserBean.class, userBean.getUsername());
  </code></pre>
  
  <p>to get the entire <code>UserBean</code> object and this is right. Here comes the problem...</p>
  
  <p>ClassB </p>
  
  <pre><code>import java.io.Serializable;
  import java.lang.annotation.Annotation;
  import java.sql.Date;
  import java.util.List;
  import java.util.ResourceBundle;
  
  import javax.enterprise.context.SessionScoped;
  import javax.faces.application.FacesMessage;
  import javax.faces.context.FacesContext;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.persistence.EntityManager;
  import javax.persistence.Query;
  
  import org.apache.log4j.Logger;
  
  import timereport.db.UserBean;
  import timereport.ClassB;
  import timereport.utils.JPAUtil;
  
  @Named("classB")
  @SessionScoped
  public class ClassB implements Serializable {
  
          @Inject private UserBean userBean;
          String throughUserBean = userBean.getUsername();
          ...
  }
  </code></pre>
  
  <p>Here, I don't know why, but I expected to get the username set when the user logs in. And when I call @Inject UserBean or @Inject ClassB I expect they to be from the same session scope and hold the UserBean object that is initialized on login. But both (classB and userBean) return NULL when I refer to them. Am I wrong in my expectations for having these objects set and is there another way to do this?</p>
  
  <p>Here is part of the UserBean class: </p>
  
  <pre><code>import java.io.Serializable;
  
  import javax.enterprise.context.SessionScoped;
  import javax.inject.Named;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  
  import com.proxiad.timereport.utils.AESEncryptDecryptUtil;
  
  @Named("user")
  @SessionScoped
  @Entity
  @Table(name="t_user")
  public class UserBean implements Serializable{
      /**
       * 
       */
      private static final long serialVersionUID = -56986575421886097L;
      @Id
      @Column(name="username")
      private String username;
      @Column(name="fullname")
      private String fullname;
      @Column(name="password")
      private String password;
      @Column(name="email")
      private String email;
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn (name="department", referencedColumnName="value")
      private ReferenceDataBean department;
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn(name="role", referencedColumnName="value")
      private ReferenceDataBean role;
  
      public UserBean() {}
  
      public String getFullname() {
          return fullname;
      }
  
      public void setFullname(String fullname) {
          this.fullname = fullname;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          try {
              this.password = AESEncryptDecryptUtil.encrypt(password);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public ReferenceDataBean getDepartment() {
          return department;
      }
  
      public void setDepartment(ReferenceDataBean department) {
          this.department = department;
      }
  
      public ReferenceDataBean getRole() {
          return role;
      }
  
      public void setRole(ReferenceDataBean role) {
          this.role = role;
      }
  
      @Override
      public int hashCode() {
          return getUsername().hashCode() + 17 * getEmail().hashCode();
      }
  
      @Override
      public boolean equals(Object obj) {
          if (obj instanceof UserBean) {
              UserBean user = (UserBean) obj;
              if (getUsername().equals(user.getUsername()) &amp;&amp; getEmail().equals(user.getEmail())) {
                  return true;
              }
          }
          return false;
      }
  
      @Override
      public String toString() {
          return String.format("Username: %s\nEmail: %s\nDepartment:\n%s\nRole:\n%s\n", 
                  getUsername(), getEmail(), getDepartment(), getRole());
      }
  
  }
  </code></pre>