package au.edu.uts.aip.mandreacchio.ejb;
import java.util.List;
import javax.persistence.*;
import au.edu.uts.aip.mandreacchio.jpa.Status;
public class StatusDAOJpaImpl implements StatusDAO {
@PersistenceContext
private EntityManager em;
// this is the default constructor
public StatusDAOJpaImpl() {
this("store");
}
public StatusDAOJpaImpl(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 StatusDAOJpaImpl(EntityManager em) {
this.em = em;
}
public void setEntityManager(EntityManager em) {
this.em = em;
}
// Begin Class Methods:
@Override
public List<Status> getAllStatus() {
try {
@SuppressWarnings("unchecked")
List<Status> results = em.createQuery("select S from Status S")
.getResultList();
// Lazy Loading workaround
for(Status s: results){
s.getId();
s.getStatus();
}
return results;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public Status getStatusById(int id) {
Status result = (Status) em.getReference(Status.class, id);
return result;
}
}