1

I want to develop a project in Spring JPA with Primefaces. I actually need suggestion of this project architecture. I have already thinked about a architecture like this...

employee.xhtml (View Page)

   <h:form>
     <p:inputText value="#{employeeController.employeePojo.id}" label="ID"/>
     <p:inputText value="#{employeeController.employeePojo.name}" label="Name"/>
     <p:commandButton value="Save" action="#{employeeController.saveEmpInfo()}"/>
   </h:form>

EmployeePojo.java (Pojo class)

  public class EmployeePojo {
    private String id;
    private String name;
    /// getter and setter also....
  }

EmployeeController.java (Controller class)

  import javax.faces.bean.ManagedBean;
  import org.springframework.stereotype.Controller;
  import org.springframework.context.annotation.Scope;
  import org.springframework.beans.factory.annotation.Autowired;

  @ManagedBean
  @Controller
  @Scope("session")
  public class EmployeeController{
     private EmployeePojo employeePojo;
     ///getter and setter of employeePojo.

    @Autowired
    EmployeeService empService;

    public void saveEmpInfo(){
      empService.saveEmployee(this.employeePojo);
    }
  }

EmployeeService.java (Service class)

   import org.springframework.stereotype.Service;
   import org.springframework.beans.factory.annotation.Autowired;

   @Service
   public class EmployeeService {

      @Autowired
      EmployeeDao empDao;

      public void saveEmployee(EmployeePojo pojo){
           Employee employee=new Employee();
           employee.setId(pojo.getId);
           employee.setName(pojo.getName);
           empDao.persist(employee);
      }
   }

EmployeeDao.interface (interface)

    public interface EmployeeDao extends EntityDao<Employee> {
    //here additional method can be decleared...
    }

EmployeeDaoImpl.java (Component and Transactional)

    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;

    @Component
    @Transactional
    public class EmployeeDaoImpl extends EntityDaoImpl<Employee> implements EmployeeDao {
       //implementation of all additional method of EmployeeDao....
    }

EntityDao.interface (interface)

    public interface EntityDao<E> {
       public void persist(E e) throws Exception;
       public void dynamicListPersists(List<E> entities) throws Exception;
       public void dynamicListMerge(List<E> listForMerge) throws Exception;
       public void merge(E e) throws Exception;
       public void remove(Object id) throws Exception;
    }

EntityDaoImpl.class

    import javax.persistence.PersistenceContext;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    public class EntityDaoImpl<E>  implements EntityDao<E>{
       @PersistenceContext(unitName="persistenceUnit")
       protected EntityManager entityManager;
       protected EntityManagerFactory emf;

       protected E instance;
       private Class<E> entityClass;


       @Transactional
       public void persist(E e) throws HibernateException{     
        getEntityManager().persist(e);
        getEntityManager();
      }
      @Transactional
      public void merge(E e) throws HibernateException{     
        getEntityManager().merge(e);
      }
      @Transactional
      public void remove(Object id) throws Exception{     
   getEntityManager().remove((E)getEntityManager().find(getEntityClass(), id));
      }

      public EntityManager getEntityManager() {
        return entityManager;
      }
      public void setEntityManager(EntityManager entityManager) throws Exception{
        this.entityManager = entityManager;
      }

      @SuppressWarnings("unchecked")
      public Class<E> getEntityClass() throws Exception{      
        if (entityClass == null) {
            Type type = getClass().getGenericSuperclass();
          if (type instanceof  ParameterizedType) 
          {
              ParameterizedType paramType = (ParameterizedType) type;
              if (paramType.getActualTypeArguments().length == 2) {
                    if (paramType.getActualTypeArguments()[1] instanceof  TypeVariable) {
                       throw new IllegalArgumentException(
                          "Can't find class using reflection");
                   }
                    else {
                       entityClass = (Class<E>) paramType.getActualTypeArguments()[1];
                  }
               } else {
                  entityClass = (Class<E>) paramType.getActualTypeArguments()[0];
                }
           } else {
              throw new Exception("Can't find class using reflection");
           }
          }
         return entityClass;
       }
    }

And Here is the entity class...

Employee.java (Entity class)

   @Entity
   @Table(name="employee")
   public class Employee {

      @Id
      @Column(name="id")
      private String id;

      @Column(name="name")
      private String name;

      // getter and setter also.....
   }

Is this a architecture of Spring JPA project?

Please suggest me...

Abu Bakar Siddik
  • 487
  • 1
  • 7
  • 16

0 Answers0