i have created a demo application Using JSF & EJB 3.0 (Stateless session bean and JPA), my persistence provider is Hibernate 4, and database is Apache Derby.
My class flow i.e sequential flow is as follows,
ManagedBean calls Stateless Session bean, in this we have a JPA calls,
please follow the code, the JSF managed bean StudentMgBean.java,
@ManagedBean(name="stMgBean")
@ViewScoped
public class StudentMgBean implements Serializable{
private static final long serialVersionUID = 109117543434170143L;
...........
@EJB
private StudentService studentService;
.........
@PostConstruct
public void init(){
..........
........
this.totalStudentInDB = studentService.getMaxStudent();
}
}
My EJB Interface StudentService.java,
@Local
public interface StudentService {
List<StudentVO> fetchStudentListOrderByStudentId(boolean flag);
List<StudentVO> fetchStudentListOrderByStudentName(boolean flag);
void saveStudentEntity(StudentEntity studentEntity,Integer deptId) throws Exception;
List<DeptEntity> fetchAllDept();
List<StudentVO> fetchStudentByDept(Integer deptId);
void saveAllStudents(List<StudentVO> students) throws Exception;
void deleteAllStudents(List<StudentVO> students) throws Exception;
List<StudentVO> fetchStudentListPerPage(Integer minRow,Integer maxRow) throws Exception;
Integer getMaxStudent() throws Exception;
}
My EJB Stateless Session bean StudentServiceBean.java,
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class StudentServiceBean implements StudentService{
@PersistenceContext(unitName="forPractise")
private EntityManager entityMgr;
@Resource
private SessionContext sessionContext;
@EJB
private DeptService deptService;
@Override
public List<StudentVO> fetchStudentListOrderByStudentId(boolean flag){
.........
}
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void saveStudentEntity(StudentEntity studentEntity,Integer deptId) throws Exception{
........
}
}
In the StudentServiceBean, i have injected EntityManager, so i directly do JPA operation in the methods written in this session bean.
No my question is can i use any design pattern in this flow, can i go for a separate DAO layer, as i am using EJB 3.0 i don't have to use ServiceLocator pattern, but than any other pattern can i use to seperate Bussiness logic with the JPA call,
One more thing, In JSF managed Bean i have properties and its getter setter methods which are mapped to the JSP componenets in EL like this value={stMgBean.studentList}
but in the same managed bean i have also have method which will be invocked by action command call from JSF, should those method be written in the seperate Managed bean ?
Please suggest a Design pattern which can be used for projects which have JSF 2.0, EJB 3.0 and JPA
Waiting for the reply