Anda di halaman 1dari 3

PAGINA 30

import javax.persistence.criteria.Root;
import modelo..exceptions.NonexistentEntityException;
import modelo.exceptions.PreexistingEntityException;

/**
*
*@author bibli
*/

public class PacientesJpaController implements Serializable {

public PacientesJpaController(EntityManagerFactory emf) {


this.emf = emf;
}

private EntityManagerFactory emf = null;

public EntityManager getEntityManager(){


return emf createEntityManager();
}

public void create(Pacientes pacientes)throws PreexistingEntityException, Exception


{
EntityManager em = null;

try {
em =getEntityManager();
em.getTransaction().begin();
em.persist(pacientes);
em.getTransaction().commit();

}
catch (Exception ex){

if(findPacientes(pacientes.getPacidentificacion()) !=null){
throw new PreexistingEntityException("Pacientes" + pacientes + "already exists." ,
ex);

throw ex;

}
finally {

if(em !=null){

em.close();

}
}
}

public void edit(Pacientes pacientes)throws NonexistentEntityException,Exception {

EntityManager em = null;

try {
em = getEntityManager();
em.getTransaction().begin();
pacientes = em.merge(pacientes);
em.getTransaction().commit();
} catch (Exception ex){

String msg = ex.getLocalizedMessage();


String id = pacientes.getPacidentificacion();
if (findPacientes(id) ==null){

throw new NonexistentEntityException("The pacientes with id"+ id + "no longer


exists.");

------------------------- INICIA PAGINA 31


}
throw ex;
} finally {

if(em != null){
em.close();
}
}
}

public void destroy(String id)throws NonexistentEntityException {

EntityManager em = null;
try {

em = getEntityManager();
em.getTransaction().begin();
Pacientes pacientes;
try{

pacientes=em.getReference(Pacientes.class,id);
pacientes.getPacidentificacion();

} catch (EntityNotFoundException enfe){


throw new NonexistentEntityException("The pacientes with id" + id +" no longer
exists.", enfe);
}
em.remove(pacientes);
em.getTransaction().commit();
} finally {
if(em != null){
em.close();
}
}
}

public List<Pacientes>findPacientesEntities(){
return findPacientesEntities(true, -1, -1);

}
public List<Pacientes>findPacientesEntities(int maxResults, int firsResult){
return findPacientesEntities(false,maxResults,firstResult);
}

private List<Pacientes>findPacientesEntities(boolean all,int maxResults,int


firstResult){
EntityManager em = getEntityManager();

try{

CriteriaQuery cq=em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Pacientes.class));
Query q=em.createQuery(cq);

if(!all){
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
}finally{
em.close();
}

-------------------------------TERMINA 31

Anda mungkin juga menyukai