package au.edu.uts.aip.mandreacchio.ejb;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import au.edu.uts.aip.mandreacchio.jpa.Customer;
public class CustomerDAOJpaImpl implements CustomerDAO {
@PersistenceContext
private EntityManager em;
// this is the default constructor
public CustomerDAOJpaImpl() {
this("store");
}
public CustomerDAOJpaImpl(String unitName) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory(unitName);
em = emf.createEntityManager();
}
// this is the special constructor to set the entity manager.
// this is used when we run with EJB3
public CustomerDAOJpaImpl(EntityManager em) {
this.em = em;
}
public void setEntityManager(EntityManager em) {
this.em = em;
}
// Start methods
@Override
public Customer createCustomer(Customer c) {
em.getTransaction().begin();
em.persist(c);
em.flush();
return c;
}
@Override
public Customer getCustmoerById(int customerId) {
@SuppressWarnings("unchecked")
Customer result = (Customer) em.getReference(Customer.class, customerId);
return result;
}
}