I am trying to implement login method with JSF, EJB and JPA. My current approach is that I am passing login and password from jsf page to managed bean, method login() in bean is then fired when user presses a button.
Then the method login is calling authenticate method in EJB bean and here comes my question. I have two types of users(two entities, Student and Teacher), so what should be the returning value of EJBs method in case that the user was found?
I was thinking about returning the id of entity and then retrieve my entity with that id in the managed bean. Or do it somehow with polymoformism, but my entities are already extending the AbstractEntity class(which contains only id and setters/getters) and I don’t see any way how it could help me with my problem. The goal is to have my user entity after succesfull login in the original managed bean.
Thanks a lot for answers, this is a maybe stupid question but I am really out of ideas:-)
You’ve basically 2 options:
Let both
StudentandTeacherextend a common abstract class likePersonorUser. That works of course only the best if there are (almost) only common methods.Create another class like
PersonorUserwhich can takeStudentorTeacheras constructor argument and let all methods delegate to the wrapped instance. You should then model this class according to what the views need. You of course can’t and don’t want to do aninstanceofin EL expressions.This can further be abstracted by creating 3 classes: one base abstract wrapper class which the views need and two concrete implementations, one which should wrap
Studentand other which should wrapTeacher. This allows for more easy extension when you want to add again another kind of user.