Show TOC

Procedure documentationDeveloping the Business Logic Layer Locate this document in the navigation structure

 

The business logic of the application is represented with a session bean named Project Facade. You need to create a separate Java package that stores the session bean and then manually add the implementation that is required.

Procedure

  1. From the context menu of the app_ejb, choose   New   EJB Session Bean 3.0  .

  2. Enter the following package name com.sap.nwce.ra.sedm.ejb.service .

  3. Enter ProjectFacade for the EJB class name and choose Finish.

  4. Inject a persistence manager field to use the Entity Manager.

    Syntax Syntax

    1. @PersistenceContext(unitName = "ems/EJB3_simpl_EDM_DEMO_PU")
          protected EntityManager em;
      
    End of the code.

    With the Entity Manager you can effectively and easily manage your application entities.

  5. Add the following implementation:

    Syntax Syntax

    1. public List< Project> findProjects(String nameDescrPtrn){
      		Query q = ("".equals(nameDescrPtrn)) ? 
      			em.createNamedQuery( "Project.getAll" ) : 
      			em.createNamedQuery( "Project.findByNameOrDescPart" ) ;
      
      		if  ( ! "".equals(nameDescrPtrn)){
      			q.setParameter("namepart", "%" + nameDescrPtrn + "%");
      			q.setParameter("namepart1", "%" + nameDescrPtrn + "%");
      		}
      
      		return q.getResultList();
      	}
      
      	public List< Employee> findEmployees(String nameDescrPtrn){
      		Query q = ("".equals(nameDescrPtrn)) ? 
      			em.createNamedQuery( "Employee.getAll" ) : 
      			em.createNamedQuery( "Employee.findByNamePart" ) ;
      
      		if  ( ! "".equals(nameDescrPtrn)){
      			q.setParameter("namepart", "%" + nameDescrPtrn + "%");
      			q.setParameter("namepart1", "%" + nameDescrPtrn + "%");
      		}
      
      		return q.getResultList();
      	}
      
      	public void saveProject(Project proj){
      		em.merge(proj);
      	}
      
      	public void saveEmployee(Employee empl){
      		em.merge(empl);
      	}
      
      	public Project getProjectById(int id){
      		Query q = em.createNamedQuery( "Project.getByProjectID" ); 
      		q.setParameter("prjId", id);
      		return (Project) q.getSingleResult();
      
      	}
      
      	public Employee getEmployeeById(int id){
      		Query q = em.createNamedQuery( "Employee.getById" ); 
      		q.setParameter("id", id);
      		return (Employee) q.getSingleResult();
      
      	}
    End of the code.
  6. From the secondary mouse button, select   Source   Organize Imports   javax.persistence.Query   java.util.List  .

  7. Add all the methods to local interface by choosing the corresponding method from the Outline view, then from its context menu choose EJB methods—> Add to local Interfaces.