If you use CDI you should use named which is a qualifier, basically what it does is make your bean accessible through EL.
The import for the scoped view should be faces.view.ViewScoped and not faces.bean.Vie wScoped
See doc
import javax.inject.Named;
import javax.faces.view.ViewScoped;
@Named(value = "loginMB")
@ViewScoped
public class LoginMB implements Serializable {
// cod...
}
@ManagedBean's JSF is already deprecated.
Ex: from communication to database
@Named(value = "loginMB")
@ViewScoped
public class LoginMB implements Serializable {
@Inject
SeuServico servico
@Transactional
public void save() {
servico.salvar(algumObjeto);
}
}
Service:
public class SeuServico implements Serializable {
@Inject
SeuRepository repository;
public AlgumObjeto salvar(AlgumObjeto AlgumObjeto) {
return repository.persiste(user);
}
}
Repository:
@Repository
public class SeuRepository implements Serializable {
private final EntityManager em;
@Inject
public SeuRepository(EntityManager em) {
this.em = em;
}
public AlgumObjeto persiste(AlgumObjeto algumObjeto) {
AlgumObjeto = em.merge(algumObjeto);
em.flush();
return AlgumObjeto;
}
}
Note: the repository annotation you will not find in java, this is because I create it, I say that my repositories are dependent on the CDI, at link has it
If you ask yourself: Why have a service layer if they do what my repositories do? is what I said above, if they are basic things like saving, findById of course has no logic at all, but if you need to validate if doing some services with nothing elegant codes, it is in the service that the dirty work should be done.
You can still consider using generic repositories, I do not like, I do not like inheritance in repositories, but it's my thing, sequiser, use it but it goes beyond the scope of the question, hugs.