title: "Unable to convert ejbRef for ejb" on CDI (Weld) injection of @Stateless EJB into @SessionScoped JSF2 bean in Glassfish
body: <p>[<strong>UPDATE</strong>: After discussion on the Glassfish forums/ML at <a href="http://forums.java.net/jive/thread.jspa?messageID=480532" rel="noreferrer">http://forums.java.net/jive/thread.jspa?messageID=480532</a> a bug was filed against Glassfish <a href="https://glassfish.dev.java.net/issues/show_bug.cgi?id=13040" rel="noreferrer">https://glassfish.dev.java.net/issues/show_bug.cgi?id=13040</a> for this issue.]</p>
  
  <p>I'm trying to inject a local no-interface view of a @Stateless EJB into a JSF2 @Named @javax.enterprise.context.SessionScoped backing bean. The EJB is one of several that extend an abstract generic base class. Injection of "@Inject TheEJBClass varName" fails with "Unable to convert ejbRef for ejb TheEJBClass to a business object of type class my.package.name.TheAbstractBase". <em>[edit: Actually, it turns out that injection succeeds, but method resolution in the injected proxy for methods inherited from superclasses fails.]</em> If I use "@EJB TheEJBClass varName" then varName remains null, ie nothing is injected.</p>
  
  <p>Details:</p>
  
  <p>I'm running Glassfish 3.0.1 on Linux (Ubuntu 10.04 in case it matters) and having real problems handling injection of my data model EJBs into my JSF2 session scoped models using CDI (Weld). And yes, before you ask, I have beans.xml in place and CDI is activating to perform injection.</p>
  
  <p>If I inject it with an @EJB annotation, eg:</p>
  
  <pre><code>@EJB TheEJBClass memberName;
  </code></pre>
  
  <p>... the EJB isn't actually injected, leaving memberName null.</p>
  
  <p>If I inject it with a CDI @Inject annotation:</p>
  
  <pre><code>@Inject TheEJBClass memberName;
  </code></pre>
  
  <p>... then CDI complains when I call a method of "memberName" that's implemented in a superclass of TheEJBClass and not overridden in TheEJBClass its self, reporting:</p>
  
  <pre><code>java.lang.IllegalStateException: Unable to convert ejbRef for ejb TheEJBClass to a business object of type class my.package.name.TheAbstractBase
      at
  com.sun.ejb.containers.EjbContainerServicesImpl.getBusinessObject(EjbContainerServicesImpl.java:104)
  at
  org.glassfish.weld.ejb.SessionObjectReferenceImpl.getBusinessObject(SessionObjectReferenceImpl.java:60)
  ....
  </code></pre>
  
  <p>I've tried converting the base to concrete class and de-generifying it, but encounter the same problem, so I don't think I'm hitting the Weld bugs with generic bases (<a href="https://jira.jboss.org/browse/WELD-305" rel="noreferrer">https://jira.jboss.org/browse/WELD-305</a>, <a href="https://jira.jboss.org/browse/WELD-381" rel="noreferrer">https://jira.jboss.org/browse/WELD-381</a>, <a href="https://jira.jboss.org/browse/WELD-518" rel="noreferrer">https://jira.jboss.org/browse/WELD-518</a>).</p>
  
  <p>An outline of the code, with full package qualification on annotations added for clarity, is:</p>
  
  <pre><code>// JSF2 managed backing bean.
  //
  // Called via #{someJSF2Model.value} in a JSF2 page
  //
  @javax.inject.Named
  @javax.enterprise.context.SessionScoped
  public class SomeJSF2Model implements Serializable {
     @javax.inject.Inject TheEJBClass member;
  
     public Integer getValue() {
         return member.getValue();
     }
     // blah blah
  }
  
  // One of several EJB classes that extend TheAbstractBase
  @javax.ejb.Stateless
  public class TheEJBClass extends TheAbstractBase {
    // blah blah
    // does **NOT** override "getValue()"
  }
  
  public abstract class TheAbstractBase {
      // blah blah
      public Integer getValue() {
          return 1;
      }
  }
  </code></pre>
  
  <p>Note that injection <em>does</em> work if I override TheAbstractBase.getValue() in TheEJBClass, or if I call a method defined in TheEJBClass and not any superclass. It seems like the issue is something to do with inheritance.</p>
  
  <p>Very similar code that used JSF2's built-in lifecycle and injection features worked, but given that this is a new project and CDI is where things are heading in the future, I thought it best to try to go for CDI. Here's what I started out with using JSF2/EJB injection, which worked:</p>
  
  <pre><code>// JSF2 managed backing bean. Using @ManagedBean and JSF2's @SessionScoped
  // instead of CDI @Named and CDI @SessionScoped this time.
  //
  @javax.faces.bean.ManagedBean
  @javax.faces.bean.SessionScoped
  public class SomeJSF2Model implements Serializable {
     @javax.ejb.EJB TheEJBClass member;
     public Integer getValue() {
         return member.getValue();
     }
     // blah blah
  }
  
  // One of several EJB classes that extend TheAbstractBase
  // Unchanged from CDI version
  @javax.ejb.Stateless
  public class TheEJBClass extends TheAbstractBase {
    // blah blah
    // does **NOT** override "getValue()"
  }
  
  // Unchanged from CDI version
  public abstract class TheAbstractBase {
      // blah blah
      public Integer getValue() {
          return 1;
      }
  }
  </code></pre>
  
  <p>I'm currently working on putting together a self-contained test case, but thought I'd fire off the question now in case this is something where I'm just doing something silly or there's a well known solution my Google-fu isn't up to finding. Why did it work with JSF2/EJB injection, but fail with CDI injection?</p>
  
  <p>( Since re-posted on the Glassfish forums as <a href="http://forums.java.net/jive/thread.jspa?threadID=152567" rel="noreferrer">http://forums.java.net/jive/thread.jspa?threadID=152567</a> )</p>