Java Tutorial/EJB3/Annotation

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

Create Your Own Annotation For EJB

File: EmployeeBean.java



import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  
  @JndiInjected("java:/TransactionManager")
  TransactionManager tm;
  public void doAction(){
    try {
      System.out.println("Is there a transaction: " + (tm.getTransaction() != null));
    } catch (SystemException e) {
      e.printStackTrace();
    }
    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);
    
    
    
  }
}