Use An Entity Object (POJO)

  1. Create the new Stateless Session EJB.

    In the Create a new EJB 3 Session Bean dialog, enter the following:

    The Bean Package field: by.iba.ejb

    The Bean Name field: PersistentCalculator

    NOTE: make sure Stateless Session Bean Type radio button is selected.

    Click the Finish button.

    Stateless Session Bean - Finish

  2. Add business method signatures to the PersistentCalculator interface:

    								
    public double add(double a, double b);
    public double subtract(double a, double b);
    public double multiply(double a, double b);
    public double divide(double a, double b);
    
    public List<Operation> getHistory();
    public void clearHistory();
    
    								

    Click the Ctrl + Shift + O to organize imports and Ctrl + S to save.

  3. Open the PersistentCalculatorBean.java file (if not already open) and modify the newly created class as shown below:

    								
    
    	@PersistenceContext(unitName = "calculator")
    	private EntityManager manager;
    	
    	@EJB private StatelessCalculator calculator;
    
    	public double add(double a, double b) {
    		return calculator.add(a, b);
    	}
    
    	public double subtract(double a, double b) {
    		return calculator.subtract(a, b);
    	}
    
    	public double multiply(double a, double b) {
    		return calculator.multiply(a, b);
    	}
    
    	public double divide(double a, double b) {
    		return calculator.divide(a, b);
    	}
    
    	public List<Operation> getHistory() {
    		ArrayList<Operation> list = new ArrayList<Operation>();
    		Query q = manager.createQuery("FROM Operation o ORDER BY o.timestamp DESC");
    		q.setMaxResults(5);
    		for (Object o : q.getResultList()) {
    			list.add((Operation) o);
    		}
    		return list;
    	}
    
    	public void clearHistory() {		
    		Query q = manager.createQuery("FROM Operation o");
    		for (Object o : q.getResultList()) {
    			manager.remove(o);
    		}
    		manager.flush(); 
    	}
    
    	@AroundInvoke
    	public Object persistOperation(InvocationContext ctx) throws Exception { 		
    		Object res = null;
    		try {
    			res = ctx.proceed();
    		} catch (Exception e) {			
    		} finally {			
    			if ((ctx.getParameters() != null) && (ctx.getParameters().length > 0)) {
    				Operation o = new Operation();
    				double operandA = new Double(ctx.getParameters()[0].toString());
    				double operandB = new Double(ctx.getParameters()[1].toString());
    				double result   = new Double(res.toString());
    				o.setOperandA(operandA);
    				o.setOperandB(operandB);
    				o.setResult(result);
    				o.setOperation(ctx.getMethod().getName());
    				o.setTimestamp(new Timestamp(new Date().getTime()));
    				manager.persist(o);
    			}	
    		}
    		return res;
    	}
    
    								

    Click the Ctrl + Shift + O to organize imports and Ctrl + S to save the file.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine