Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot inject bean of non-serializable type into bean of passivating scope

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

like image 732
Pepe Fernandez Avatar asked Jun 30 '15 12:06

Pepe Fernandez


1 Answers

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:

  1. Ignore it. It'll run perfectly fine.

  2. Bow for the false error and let EJB class implement Serializable anyway.

  3. 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.

  4. 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.

like image 112
BalusC Avatar answered Sep 27 '22 17:09

BalusC