
Untitled
By: a guest on
Jun 17th, 2012 | syntax:
None | size: 1.62 KB | hits: 15 | expires: Never
What can AOP do that OOP can't do?
public void saveMyEntityToTheDatabase(MyEntity entity) {
EntityTransaction tx = null;
try {
tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(entity);
tx.commit();
} catch (RuntimeException e) {
if(tx != null && tx.isActive()) {
tx.rollback();
}
throw e;
}
}
@Transactional
public void saveMyEntityToTheDatabase(MyEntity entity) {
entityManager.persist(entity);
}
public void saveMyEntityToTheDatabase(MyEntity entity) {
database.performInTransaction(new Save(entity));
}
@Transactional
public Employee hire(Person p, Employee manager, BigDecimal salary) {
// implementation omitted
}
Employee hired = service.hire(candidate, manager, agreedOnSalary);
class HireAction implements Callable<Employee> {
private final Person person;
private final Employee manager;
private final BigDecimal salary;
public HireAction(Person person, Employee manager, BigDecimal salary) {
this.person = person;
this.manager = manager;
this.salary = salary;
}
@Override
public Employee call() throws Exception {
// implementation omitted
}
}
Employee hired = session.doInTransaction(new HireAction(candidate, manager, agreedOnSalary));
class HRService {
class HireAction {
// impl omitted
}
class FireAction {
// impl omitted
}
}
Employee emp = session.doInTransaction(new HRService().new HireAction(candidate, manager, salary));
Employee e = new HireAction(candidate, manager, salary).call();