Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:42:43+00:00 2026-05-27T00:42:43+00:00

I have the following classes: ClassA import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.inject.Inject; import javax.inject.Named;

  • 0

I have the following classes:

ClassA

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
}

Here in login() I am doing

EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());

to get the entire UserBean object and this is right. Here comes the problem…

ClassB

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

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?

Here is part of the UserBean class:

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:42:44+00:00Added an answer on May 27, 2026 at 12:42 am

    Injection takes place after construction. Injected properties are not available during construction. The earliest point to access injected properties is a @PostConstruct method.

    So, replace

    @Inject private UserBean userBean;
    String throughUserBean = userBean.getUsername();
    

    by

    @Inject private UserBean userBean;
    String username;
    
    @PostConstruct
    public void init() {
        username = userBean.getUsername();
    }
    

    That @Inject ClassB makes no sense, so I removed it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: import play.db.ebean.Model; import javax.persistence.*; @Entity public class A extends
I have the following classes: Main.java package com.example.webapp; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity;
I have the following two classes: import java.io.*; import java.util.*; public class User {
I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue
with C#, java or c++ ... we have the following classes class A {
I have Java classes with the following structure (the class names do not imply
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I have the following two classes: package { import flash.display.Sprite; import flash.events.Event; public class
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have the following directory structure. src| |pack| |Test.java data| |data.txt classes| |pack| |Test.class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.