Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.62 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What can AOP do that OOP can't do?
  2. public void saveMyEntityToTheDatabase(MyEntity entity) {
  3.     EntityTransaction tx = null;
  4.     try {
  5.         tx = entityManager.getTransaction();
  6.         tx.begin();
  7.         entityManager.persist(entity);
  8.         tx.commit();
  9.     } catch (RuntimeException e) {
  10.         if(tx != null && tx.isActive()) {
  11.             tx.rollback();
  12.         }
  13.         throw e;
  14.     }
  15. }
  16.        
  17. @Transactional
  18. public void saveMyEntityToTheDatabase(MyEntity entity) {
  19.     entityManager.persist(entity);
  20. }
  21.        
  22. public void saveMyEntityToTheDatabase(MyEntity entity) {
  23.     database.performInTransaction(new Save(entity));
  24. }
  25.        
  26. @Transactional
  27. public Employee hire(Person p, Employee manager, BigDecimal salary) {
  28.     // implementation omitted
  29. }
  30.        
  31. Employee hired = service.hire(candidate, manager, agreedOnSalary);
  32.        
  33. class HireAction implements Callable<Employee> {
  34.     private final Person person;
  35.     private final Employee manager;
  36.     private final BigDecimal salary;
  37.  
  38.     public HireAction(Person person, Employee manager, BigDecimal salary) {
  39.         this.person = person;
  40.         this.manager = manager;
  41.         this.salary = salary;
  42.     }
  43.  
  44.     @Override
  45.     public Employee call() throws Exception {
  46.         // implementation omitted
  47.     }
  48. }
  49.        
  50. Employee hired = session.doInTransaction(new HireAction(candidate, manager, agreedOnSalary));
  51.        
  52. class HRService {
  53.     class HireAction {
  54.         // impl omitted
  55.     }
  56.     class FireAction {
  57.         // impl omitted
  58.     }
  59. }
  60.        
  61. Employee emp = session.doInTransaction(new HRService().new HireAction(candidate, manager, salary));
  62.        
  63. Employee e = new HireAction(candidate, manager, salary).call();