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 6775077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:51:50+00:00 2026-05-26T15:51:50+00:00

My idea is : I have a bean ( UserBean ) which have some

  • 0

My idea is : I have a bean (UserBean) which have some attributes to help me validate before I set into the entity User. I’m doing that to reuse the validate in the UserBean attribute in the whole project and to keep separated the layers as I could read in a lot of posts here in the stack overflow.
Knowing that I would like to keep the MVC structure, let’s see what I do so far:

structure

( Sorry about the big picture I thought it would be more easier understand my question)

This is my User (in entity package):

@Entity
@Table(name="user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private int id;

    private String email;

    private String password;

    private int reputation;

    //bi-directional one-to-one association to Company
    @OneToOne(mappedBy="user", cascade={CascadeType.ALL})
    private Company company;

    //bi-directional many-to-one association to Location
    @OneToMany(mappedBy="user")
    private List<Location> locations;

    //bi-directional one-to-one association to Person
    @OneToOne(mappedBy="user")
    private Person person;

    //bi-directional many-to-one association to Product
    @OneToMany(mappedBy="user")
    private List<Product> products;

    //bi-directional many-to-one association to UserType
    @ManyToOne
    @JoinColumn(name="type")
    private UserType userType;

    //bi-directional many-to-one association to UserPhone
    @OneToMany(mappedBy="user")
    private List<UserPhone> userPhones;

    //bi-directional many-to-one association to UserPicture
    @OneToMany(mappedBy="user")
    private List<UserPicture> userPictures;

    //bi-directional many-to-one association to UserSocialNetwork
    @OneToMany(mappedBy="user")
    private List<UserSocialNetwork> userSocialNetworks;

       // getters and setters

}

And this is his bean , UserBean (in bean.entity package):

@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
    private static final long serialVersionUID = -1626847931067187536L;

    private int id;

    @NotNull(message="informe seu e-mail")
    @Email(message="e-mail inválido")
    private String email;

    @NotNull(message = "6 dígitos no mínimo")
    @Size(min = 6, max = 128, message = "6 dígitos no mínimo")
    private String password;

    @NotNull(message = "6 dígitos no mínimo")
    @Size(min = 6, max = 128, message = "6 dígitos no mínimo")
    private String password_2;

    private int reputation;

    private UserType userType;

    private List<UserPhoneBean> userPhones;

    private List<UserPicture> userPictures;

    private List<UserSocialNetwork> userSocialNetworks;

    @AssertTrue(message = "senhas diferentes")
    public boolean isPasswordsEquals() {
        return password.equals(password_2);
    }

        // getters and setters

}

Then every action that has to do with the User I would like to keep the control in one place, so I create the UserControl bean:

@ManagedBean(name="userc")
@ViewScoped
public class UserControl implements Serializable {
    private static final long serialVersionUID = -1626847931067187536L;

    @EJB EaoUser eaoUser;
    @EJB EaoPerson eaoPerson;

    @ManagedProperty("#{userBean}")
    private UserBean user;

    @ManagedProperty("#{personBean}")
    private PersonBean person;

    private UserBean ub;
    private PersonBean pb;

    private View view;


    public UserControl() {
        ub = new UserBean();
        pb = new PersonBean();
        view = new View();
    }

    public String login(){
        List<User> list = eaoUser.findByEmail(ub.getEmail());
        User u = list.get(0);

            if (Crypto.check(ub.getPassword(), u.getPassword())){
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); 
                session.setAttribute("authenticated", true);

                user = new UserBean();
                user.setId(u.getId());
                user.setEmail(u.getEmail());
                user.setPassword(u.getPassword());
                user.setReputation(u.getReputation());

                user.setUserType(u.getUserType());

                if (u.getUserType().getId() == 10){
                    Person p = eaoPerson.find(u.getId());

                    if (p != null){
                        person = new PersonBean();
                        person.setName(p.getName());
                        person.setSurname(p.getSurname());
                        person.setBirthdate(p.getBirthdate());
                        person.setGender(p.getGender());
                    }
                }

                for (UserPhone up : u.getUserPhones()){
                    user.getUserPhones().add(new UserPhoneBean(up.getId(), up.getPhone()));
                }

                user.setUserPictures(u.getUserPictures());
                user.setUserSocialNetworks(u.getUserSocialNetworks());

            }else{
                view.imageError();
                view.setStatus("e-mail ou senha incorretos");
                view.setPopup(true);

                return "#";
            }

        return "secured/user/home?faces-redirect=true";
    }
        // others method, getters and setters
}

Note that I’m injecting the userBean here in userControl, so when the user log in I rescue the value from the database and set in the userBean.

I create the attribute user and ub because when I try to validate with the user the validation it seems to not work, I guess it is because he’s injected already (just a guess).

So, after all this explanation, when I try to log in, the user (whic is injected) don’t keep any value anymore.

If I try to do this:

Welcome, <h:outputText value="#{userc.user.email}" />

Don’t appears nothing..

So I wonder if my structure is correct.
What you guys think about it ?

  • 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-26T15:51:51+00:00Added an answer on May 26, 2026 at 3:51 pm

    That problem has nothing to do with the project structure.

    The real problem is here, in the login() method:

    user = new UserBean();
    

    You’re overriding a JSF-injected managed bean by a manually created bean. This one won’t be used by JSF and gets lost after the view leaves. The JSF-injected managed property user shouldn’t be null at that point. You don’t need to create it yourself. Remove that line altogether.

    But you’ve other bigger problems, that copying of properties from one User to other User is totally unnecessary. Basically, you need to add a User property to the UserBean class and do a user.setUser(user); instead. This way it’s available by #{userBean.user.email}. I would only maybe rename the one or other so that the EL code makes more sense. You’d like to end up as something like #{user.current.email} or #{login.user.email} or #{auth.user.email}, etc.

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

Sidebar

Related Questions

do you have idea, if there would be some nice way to browse/log JMS
I have created the following entity bean, and specified two columns as being unique.
I have a singleton ejb which is getting initialised twice. I have no idea
First, I have a stateless bean which do a simple retreive, looking like this.
Anyone have idea about how to bind Boolean in sqlite with objective - c.
I have idea to write errors from my application to the Windows Event Viewer
Anyone have idea how to pass value from iPhone application to the REST-Webservice using
Simple idea: I have two images that I want to merge, one is 500x500
The Idea I have a main PHP page that loads content in DIV's from
Any body have idea how to put controls dynamically in asp.net 3.5 ? if

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.