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