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.
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!FullyQualifiedNameOfRemoteInterface");
Thanks @Andre!
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With