I am working on a Java Enterprise application which have an EJB layer and a WEB layer bundled together with an EAR. The problem I am facing is that I have some entities in my EJB layer and when I try to get their EAOs by JNDI in my WEB layer, I get NameNotFoundException.
My Ear name is : StudentManagement
My EJB layer is : StudentManagementEJB
My Web layer is : StudentManagementWEB
Application Server : JBOSS-4.2
My Entity Class :
@Entity
@Table(name="student")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="student_id")
private int studentId;
@Column(name="sudent_name")
private String studentName;
@Column(name="student_class")
private String studentClass;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}
}
My Remote Inteface :
@Remote
public interface StudentEAO {
public void save(Student student);
}
My Interface Implementation:
@Stateless
public class StudentEAOImpl implements StudentEAO {
@PersistenceContext
private EntityManager entityManager;
/* (non-Javadoc)
* @see com.learn.interfce.StudentEAO#save(com.learn.entity.Student)
*/
@Override
public void save(Student student) {
// TODO Auto-generated method stub
try{
entityManager.persist(student);
}
catch(Exception e){
e.printStackTrace();
}
}
}
And In my servlet, I am trying to get StudentEAOImpl Object as
try {
StudentEAO studentEao = (StudentEAO) new InitialContext().lookup("StudentManagement/StudentEAOImpl/remote");
studentEao.save(student);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StackTrace of ERROR :
01:15:45,539 INFO [STDOUT] Hello World from StudentActionServlet.
01:15:45,543 ERROR [STDERR] javax.naming.NameNotFoundException: StudentManagement not bound
01:15:45,543 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
01:15:45,543 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
01:15:45,543 ERROR [STDERR] at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
01:15:45,543 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
01:15:45,543 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:628)
01:15:45,543 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
01:15:45,543 ERROR [STDERR] at javax.naming.InitialContext.lookup(InitialContext.java:392)
01:15:45,543 ERROR [STDERR] at com.learn.servlets.StudentActionServlet.doGet(StudentActionServlet.java:46)
I am not getting what I am missing here. Please help me in this regard.
Thanks!
Atlast! Issue resolved and here are my findings about that:
1- You have to check your EJB, WEB and EAR layer compiler level, It all should be same.
2- Check your EAR name while giving JNDI name , means if your JNDI name is : MyEar/MYEJB/remote, then MyEar should be your EA-Project name.
3- You server log must be clean. It should not produce any critical exception which may be show-stopper.
After doing all these checks and fixes, my issue is resolved. Hopefully it will help someone.