2014-04-26 194 views
0

我正在研究一個Java Enterprise應用程序,它具有與EAR捆綁在一起的EJB層和WEB層。我面臨的問題是,我的EJB層中有一些實體,當我嘗試通過JNDI在我的WEB層獲取EAO時,我得到了NameNotFoundException。NameNotFoundException Java企業應用程序

我耳邊的名字是:StudentManagement

我EJB層:StudentManagementEJB

我的Web層:StudentManagementWEB

應用服務器:JBOSS-4.2

我的實體類:

@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; 
} 
} 

我的遙遠的Inteface:

@Remote 
public interface StudentEAO { 

public void save(Student student); 

} 

我的接口實現:

@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(); 
    } 
} 

} 

並在我的servlet,我試圖讓StudentEAOImpl對象爲

try { 
     StudentEAO studentEao = (StudentEAO) new InitialContext().lookup("StudentManagement/StudentEAOImpl/remote"); 
     studentEao.save(student); 
    } catch (NamingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

堆棧跟蹤錯誤的:

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) 

我沒有得到什麼,我在這裏失蹤。在這方面請幫助我。

謝謝!

回答

0

Atlast!問題已解決,這裏是我的發現:

1-您必須檢查您的EJB,WEB和EAR層編譯器級別,它們應該都是相同的。

2-在提供JNDI名稱時檢查EAR名稱,如果您的JNDI名稱是MyEar/MYEJB/remote,則表示MyEar應該是您的EA項目名稱。

3-您的服務器日誌必須是乾淨的。它不應該產生任何關鍵的例外,這可能會阻止顯示。

完成所有這些檢查和修復後,我的問題就解決了。 希望它能幫助別人。

0

嘗試手動綁定您EAOImpl類,如下所示:

@RemoteBinding(jndiBinding = 「StudentManagement/StudentEAOImpl /遠程」)