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

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

package au.edu.uts.aip.mandreacchio.ejb;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

import au.edu.uts.aip.mandreacchio.jpa.Title;

public class TitleDAOJpaImpl implements TitleDAO {

  @PersistenceContext
  private EntityManager em;

  // this is the default constructor
  public TitleDAOJpaImpl() {
    this("store");
  }

  public TitleDAOJpaImpl(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 TitleDAOJpaImpl(EntityManager em) {
    this.em = em;
  }

  public void setEntityManager(EntityManager em) {
    this.em = em;
  }

  // Begin Class Methods:
  @Override
  public List<Title> getAllTitles() {
    @SuppressWarnings("unchecked")
    List<Title> results = em.createQuery("select T from Title T")
        .getResultList();
    return results;
  }

  @Override
  public Title getTitleById(int id) {
    @SuppressWarnings("unchecked")
    Title result = (Title) em.getReference(Title.class, id);
    return result;
  }

}
TOP

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

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.