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:

( 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 ?
That problem has nothing to do with the project structure.
The real problem is here, in the
login()method: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
usershouldn’t benullat 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
Userto otherUseris totally unnecessary. Basically, you need to add aUserproperty to theUserBeanclass and do auser.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.