Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNDI lookup not working with EJB 3.x

I'm using 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 is

public String myMethod()    {

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

Here I'm getting a

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

The JNDI lookup name 'UserBean' seems to be correct. No idea what the problem is. Can anyone please help? I've deployed my application on weblogic 12c, using JPA 2.0 and EJB 3.x

Thanks in advance.

like image 225
Aditya Avatar asked Feb 18 '13 14:02

Aditya


3 Answers

The problem was I was using a Remote Interface. Using simply the @stateless annotation without mapped name The following code worked:

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

Thanks @Andre!

like image 117
Aditya Avatar answered Nov 08 '22 12:11

Aditya


It might be a better idea to make use of Portable JNDI Name, i.e. just annotate with @Stateless

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

like image 1
Andre Avatar answered Nov 08 '22 12:11

Andre


Though this question has been answered two years ago, I would like to add a comment on this. There should have been no problem in using the mappedName attribute. If you were deploying on WebLogic, you have to add #[fully.qualified.interface.name] in your lookup.

e.g. mappedName = "UserBean", the EJB is implementing an interface named MyInterface in the package com.acme.user, then the lookup would be like

... = new InitialContext().lookup("UserBean#com.acme.user.MyInterface");
like image 1
wls Avatar answered Nov 08 '22 12:11

wls