Java Tutorial/EJB3/Interceptors

Материал из Java эксперт
Перейти к: навигация, поиск

Interceptors

File: EmployeeBean.java



import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  
  @Interceptors(Profiler.class)
  public void doAction(){
    Customer cust = new Customer();
    cust.setLastName("Bond");
    cust.setSsn(1L);
    manager.persist(cust);
    
    System.out.println("Saved");
    
    cust = manager.find(Customer.class,1L);
    System.out.println(cust.getLastName());
    
    cust.setLastName("new name");
    
    manager.persist(cust);
    
    cust = manager.find(Customer.class,1L);
    System.out.println(cust.getLastName());
    
    manager.remove(cust);
    
    
    
  }
}