Monday 4 May 2009

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

FILE: StudentEJB.java
package bmpex;

import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;

public class StudentEJB implements EntityBean {
protected EntityContext ctx;
/* persistent state */
private String studentId;
private String firstName;
private String lastName;
private String address;
/* Accessor methods for entity bean fields */
public String getStudentId() {
return studentId;
}
public void setStudentId(String id) {
studentId = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/* Callback methods */
public void setEntityContext(EntityContext ctx) {
System.out.println("Student.setEntityContext called");
this.ctx = ctx;
}
public void unsetEntityContext() {
System.out.println("Student.unsetEntityContext called");
this.ctx = null;
}
public void ejbActivate() {
System.out.println("Student.ejbActivate() called.");
studentId = (String) ctx.getPrimaryKey();
}
public void ejbPassivate() {
System.out.println("Student.ejbPassivate () called.");
studentId = null;
}
public void ejbLoad() {
try {
System.out.println("Student.ejbLoad() called.");
StudentDAO dao = getDAO();
StudentDetails student = dao.load(studentId);
firstName = student.getFirstName();
lastName = student.getLastName();
address = student.getAddress();
} catch(Exception ex) {
throw new EJBException("ejbLoad: Unable to load Student "
+ studentId + " from database", ex);
}
}
public void ejbStore() {
try {
System.out.println("Student.ejbStore() called.");
StudentDAO dao = getDAO();
dao.store(studentId,firstName, lastName,address);
} catch(Exception ex) {
throw new EJBException("Student " + studentId +
" failed to save to database", ex);
}
}
public String ejbCreate(String studentId, String firstName,
String lastName, String address)
throws CreateException {
try {
System.out.println("Student.ejbCreate() called.");
StudentDAO dao = getDAO();
dao.create(studentId, firstName, lastName, address);
setStudentId(studentId);
setFirstName(firstName);
setLastName(lastName);
setAddress(address);
return studentId;
} catch(Exception ex) {
throw new CreateException("ARSFailed to create student "
+ studentId + ex.getMessage());
}
}
public void ejbPostCreate(String studentId, String firstName,
String lastName, String address) throws CreateException {
System.out.println("Student.ejbPostCreate() called.");
}
public void ejbRemove() throws RemoveException {
try {
System.out.println("Student.ejbRemove() called.");
StudentDAO dao = getDAO();
dao.remove(studentId);
} catch(Exception ex) {
throw new RemoveException("Failed to remove student "
+ studentId + ex.getMessage());
}
}
/* Finder methods */
public String ejbFindByPrimaryKey(String studentId)
throws FinderException {
try {
StudentDAO dao = getDAO();
boolean result = dao.findByPrimaryKey(studentId);
if(!result) throw new ObjectNotFoundException
("Student id "+ studentId+ " not found");
return studentId;
} catch(Exception ex) {
throw new ObjectNotFoundException
("Student id "+ studentId+ " not found");
}
}
public Collection ejbFindByLastName(String lastName)
throws FinderException {
try {
StudentDAO dao = getDAO();
return dao.findByLastName(lastName);
} catch(Exception ex) {
throw new FinderException
("FinderException:"+ lastName+ ex.getMessage());
}
}
/* Home methods */
public int ejbHomeGetTotalNumberOfStudents() {
try {
StudentDAO dao = getDAO();
return dao.findTotalNumberOfStudents();

} catch(Exception ex) {
throw new EJBException
("\\nejbHomeGetTotalNumberOfStudents:" + ex.getMessage()+ " " + ex.toString());
}
}
/* Helper methods */
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());
}
}
}