3
votes

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

1
Don't use DAOs if you don't have several different persistence providers. This will overcomplicate the architecture.Adrian Mitev
OK, but does my set of class flow is sufficient for a big project, or should i split up managed bean into two for action command methods ans separate class for getter and setter mapped properties, what should be the class hierarchy in Bussiness layer where i have used EJB's, waiting for the replyRahul Shivsharan
You should have 2 type of managed beans - controllers (execute ui logic and call business layer) and entities (jpa @Entities) that just transport the data without any logic inside. There's no need of hierarchies in the business layer -if you have something to reuse move it in a separate component and use it in multiple placesAdrian Mitev

1 Answers

0
votes

You can split the JSF layer using the next concepts:

  • Put all the data which are going to be shared between the java side and the view into specific managed beans called "Models". Now you can manage the scope of the data independently from the scope of the rest of managed beans.
  • Use the command pattern which delegates all the actions which will modified the model to the commands. The commands may invoke the EJB layer, or just update the models without going to the next layer.
  • Keep in the managed beans just the logic that you need to initialise the JSF components in the views or manage their behaviour, a reference to the model, and a reference to a delegator which will provide the command to run.