I'm learning Java EE 7.
I'm trying to store the user session in a @SessionScoped
Backing Bean but my IDE is telling me that I have an error because "Cannot inject bean of non-serializable type into bean of passivating scope".
The @SessionScoped
bean:
import negocio.Autenticacion;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
@Named
@SessionScoped
public class UserSesion implements Serializable{
@Inject
private Autenticacion auth; // Error by IDE
}
@Stateless EJB code:
import modelo.Usuario;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.security.MessageDigest;
@Stateless
public class AutenticacionBean implements Autenticacion{
@PersistenceContext(unitName = "Banco-PU")
private EntityManager em;
...
Why can't I Inject the EJB in the backing bean?
IDE: Intellij IDEA 14.1.4
This is a false error. The IDE in question apparently isn't smart enough to detect that it's actually an EJB, not a "simple" CDI (or JSF) managed bean. EJBs are always implicitly serializable.
You've 4 options:
Ignore it. It'll run perfectly fine.
Bow for the false error and let EJB class implement Serializable
anyway.
Use @javax.ejb.EJB
instead of @javax.inject.Inject
to inject it. It'll also inject the EJB, but the average IDE must be smart enough to not complain about serialization this way, because the IDE now knows for sure that it's actually an EJB, not a CDI managed bean.
Upgrade IDE to a newer version where this is fixed, if any. The ability to use @Inject
instead of @EJB
on EJBs is new since Java EE 7 (althouth the support is less complete; e.g. referencing self in @Asynchronous
won't work when using @Inject
). If still not fixed in latest IDE version, even though it claims to be Java EE 7 compatible, report a bug to them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With