0

I am using EJB 3.1 and jersey for a restapi. I would like to have a SuperResource as below, which is then inherited by the actual rest resources as below. The way I have it now, my @EJB object is null. Does anyone know how to fix this?

@Stateless
public class SuperResource {
    @EJB
    protected GenericDao<DomainObject> dao;
    . . .
}


public class MyResource extends SuperResource{     
    public String doGet(){
       return dao.get(...);
   }
}

I have tried the whole truth table between @Stateless and @Local, and SuperResource and MyResource. None of the permutations seems to work.

I don't know if that's important, my server is Glassfish 3.1.2

EDIT TO ADD DETAILS:

I didn't think so , but it seems that more detail may be necessary here:

Structure of my application:

@Local
public interface GenericDao<T extends DomainObject> {…}

public interface LoginDao  extends GenericDao<Login>{...}

@Stateless
public class GenericDaoImpl<T extends DomainObject> implements GenericDao<T> {…}

@Stateless
public class LoginDaoImpl extends GenericDaoImpl<Login> implements LoginDao {…}

@Entity
public class Login implements java.io.Serializable, DomainObject {…}

What works:

@Stateless
@Path("mypath")
public class MyResource{   
@EJB
private LoginDao dao;  
  public String doGet(){
    return dao.get(...);
  }
}
kasavbere
  • 5,873
  • 14
  • 49
  • 72

2 Answers2

0

MyResource has to be an EJB bean as well, by annotating it with @Stateless:

@Stateless
public class MyResource extends SuperResource{     
    public String doGet(){
       return dao.get(...);
   }
}

If you only need the injected DAO, you could choose to inject your JAX-RS resource with that DAO using CDI instead. If you resources -becomes- a stateless bean, then this has certain consequences that you need to be aware of (like a transaction that starts for every method, unless you explicitly disable that, etc).

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • I have edited above to add details (not sure if they are necessary). I am also adding (as comment) the error I get after doing what you suggested. – kasavbere Oct 07 '12 at 21:57
  • SEVERE: EJB5070: Exception creating stateless session bean : [GenericDaoImpl] WARNING: EJB5184:A system exception occurred during an invocation on EJB GenericDaoImpl, method: public void ccv4.dao.impl.GenericDaoImpl.save(ccv4.domain.utils.DomainObject) WARNING: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454) at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547) – kasavbere Oct 07 '12 at 21:57
  • at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) – kasavbere Oct 07 '12 at 21:58
  • My knowledge of CDI is limited to pretty much what I have in my post. Do you mind editing your post to show an example? – kasavbere Oct 07 '12 at 22:00
0

It looks like EJB injection issue. Depending on Server, you need to play around mappedName/name/beanName

What I can confirm is that, the following code works on JBoss 7.1.1.

        @Local
        public interface HelloWorldLocal {
            public String sayHello();
        }

        @Stateless
        public class HelloWorldBean implements HelloWorldLocal {
            public String sayHello(){
                return "Hello..............................................................................";
            }
        }

        @Remote
        public interface BaseRemote {
            public String test();
        }

        @Stateless
        public class BaseBean implements BaseRemote{
            @EJB
            HelloWorldLocal bean;
            public String test() {
                return "Base Bean";
            }
        }

        @Remote
        public interface DerivedRemote {
            String test();
        }

        @Stateful
        public class DerivedBean extends BaseBean implements DerivedRemote{
            public String test() {
                return bean.sayHello();
            }
        }
Maddy
  • 923
  • 5
  • 8