Java Tutorial/EJB3/Entity Bean Listener

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

Entity Bean Listener PostLoad

File: EmployeeBean.java



import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  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);
  }
}





Entity Bean Listener PostPersist

File: EmployeeBean.java



import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  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);
  }
}





Entity Bean Listener PostRemove

File: EmployeeBean.java



import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  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);
  }
}





Entity Bean Listener PostUpdate

File: EmployeeBean.java



import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  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);
  }
}