JNDI lookup does not work with EJB 3.x

I use the following Bean class:

@Stateless(name="UserBean", mappedName="UserBean")
@LocalBean
public class User implements UserRemote {

@PersistenceContext
private EntityManager em;

public User() {

}

public String login(String username, String password) {

    Query query = em.createQuery("...");
    return "xyz";
}

}

And my method

public String myMethod()    {

    try {
        User user = (User) new InitialContext().lookup("UserBean");
        return "xyz";
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return null;        
}

Here i get

javax.naming.NameNotFoundException: Unable to resolve 'UserBean'. Resolved ''; remaining name 'UserBean'

JNDI's search name 'UserBean' seems correct. I don’t know what the problem is. Can anybody help? I deployed my application on weblogic 12c using JPA 2.0 and EJB 3.x

Thanks in advance.

+5
source share
3 answers

The problem was using the remote interface. Using simply @stateless annotation without a matching name, the following code worked:

new InitialContext().lookup("java:global/ProjectName/ModuleName/BeanName!FullyQualif‌​iedNameOfRemoteInterface"); 

Thanks @Andre!

+4
source

It might be better to use the portable JNDI name, i.e. just comment using@Stateless

https://blogs.oracle.com/kensaks/entry/application_specified_portable_jndi_names

+1
source

Although this question was answered two years ago, I would like to add a comment on this issue. There should have been no problem using the mappedName attribute. If you deployed to WebLogic, you need to add #[fully.qualified.interface.name]to your search.

eg. mappedName = "UserBean",EJB implements an interface called MyInterface in the package com.acme.user, then the search will look like

... = new InitialContext().lookup("UserBean#com.acme.user.MyInterface");
+1
source

All Articles