Java EE无法解析引用Local ejb-ref

时间:2016-09-18 20:48:14

标签: java java-ee ejb java-ee-7

我收到以下错误:

Severe:   Exception while deploying the app [snel-transport-web] : Cannot resolve reference Local ejb-ref name=UserFacade,Local 3.x interface =null,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session

我用谷歌搜索了错误,我在Stack Overflow上得到了以下问题:

EJB Injection failure on deploy

Getting Cannot resolve reference Local ejb-ref not implementing parent interface

我认为它与我的注释@remote@LocalBean有关。

以下是我目前的代码:

AbstractFacade.java:

@Remote
public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }  
}

UserFacade.java:

@Stateless
@LocalBean
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName = "com.mycompany_snel-transport-ejb_ejb_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }   
}

AuthCtrl.java:

@Path("/auth")
@ApplicationPath("/api")
public class AuthCtrl extends Application {

    com.mycompany.UserFacade userFacade = lookupUserFacadeBean();

    /**
     * 
     * @return 
     */
    @GET
    @Path("/user")
    public String getHelloMsg() {
        return "test";
    }

    /**
     * 
     * @param message
     * @return
     */
    @POST
    @Path("/register")
    public String register(String message) {
        // Register user
//        userFacade.create(entity);

        return "done registering";
    }

    private com.mycompany.UserFacade lookupUserFacadeBean() {
        try {
            Context c = new InitialContext();
            return (com.mycompany.UserFacade) c.lookup("java:global/com.mycompany_snel-transport-ear_ear_1.0-SNAPSHOT/com.mycompany_snel-transport-ejb_ejb_1.0-SNAPSHOT/UserFacade!com.mycompany.UserFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你有LocalBean实现Remote接口(在这种情况下扩展了类,但它仍然适用)。尝试将Remote更改为LocalLocalBean更改为RemoteBean。如果这对于完全从抽象类中删除注释没有帮助。

答案 1 :(得分:0)

尝试重命名Bean,如果收到相同的错误消息(带有旧的Bean名称),则可能是从xml配置文件中引用了该Bean。我收到了与您相同的错误消息,问题是netbeans以某种方式在web.xml文件中生成了对我的bean的引用。