Monday 4 May 2009

3-How to Develop Bean-Managed-Persistence Entity Beans

My first difficulty was:
C:\BMPentity>ant run
Buildfile: build.xml

copy-resources:

compile:

run:
[java] JVM args ignored when same JVM is used.
[java] Starting Client . . .
[java] Looking up the student home via JNDI.
[java] Creating a new student id:raghava.
[java] javax.ejb.CreateException: Failed to create student raghavanull
[java] at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:234)

I first decided to localize and simplify the problem. So I commented most of the Client.java and left only:
print("Finding the total number of students using " +
" getTotalNumberOfStudents method");
int count = studentHome.getTotalNumberOfStudents();
print("Count:" + count);

Second, The use of try catches was causing problem in getting the error messages so I changed in StudentEJB.java:
public int ejbHomeGetTotalNumberOfStudents() {
try {
StudentDAO dao = getDAO();
return dao.findTotalNumberOfStudents();

} catch(Exception ex) {
throw new EJBException
("\\nejbHomeGetTotalNumberOfStudents:" + ex.getMessage()+ " " + ex.toString());
}
}
This helped me to find that: I must use java:comp/env/ in the look up. By the way, for testing purposes you do not have to lookup the DAO name, you can just type it and use Class.forName in order to localize problems one has to seperate the structural sections of an application.
private StudentDAO getDAO() {
try{
StudentDAO dao = null;
Context context = new InitialContext();
// dao = (StudentDAO) Class.forName ("bmpex.StudentDAOPB").newInstance();
dao = (StudentDAO) Class.forName
((String)context.lookup("java:comp/env/param/StudentDAOClass")).newInstance();
return dao;
} catch(Exception ex) {
throw new EJBException ("getDAO:" + ex.getMessage());
}
}