Package au.edu.uts.aip.mandreacchio.ejb

Source Code of au.edu.uts.aip.mandreacchio.ejb.CustomerDAOJpaImpl

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;
  }

}
TOP

Related Classes of au.edu.uts.aip.mandreacchio.ejb.CustomerDAOJpaImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.