Sei sulla pagina 1di 3

package com.libreria.

modelo;
// Generated 20-may-2015 9:04:13 by Hibernate Tools 4.3.1
import java.util.ArrayList;
import
import
import
import

javax.ejb.Stateless;
javax.persistence.EntityManager;
javax.persistence.PersistenceContext;
javax.persistence.Query;

import jdk.nashorn.internal.runtime.ListAdapter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Home object for domain model class Libro.
* @see com.libreria.modelo.Libro
* @author Hibernate Tools
*/
@Stateless
public class LibroHome {
private static final Log log = LogFactory.getLog(LibroHome.class);
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void persist(Libro transientInstance) {
log.debug("persisting Libro instance");
try {
entityManager.persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void remove(Libro persistentInstance) {
log.debug("removing Libro instance");
try {
entityManager.remove(persistentInstance);
log.debug("remove successful");
} catch (RuntimeException re) {
log.error("remove failed", re);
throw re;
}
}
public Libro merge(Libro detachedInstance) {

log.debug("merging Libro instance");


try {
Libro result = entityManager.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public Libro findById(String id) {
log.debug("getting Libro instance with id: " + id);
try {
Libro instance = entityManager.find(Libro.class, id);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
//METODOS AGREGADOS
public ArrayList<Libro> findAll()
{
ArrayList<Libro> lista=null;
try{
lista = (ArrayList<Libro>) entityManager.createQuery("fr
om Libro where eliminado = 0").getResultList();
}catch(RuntimeException ex)
{
System.out.println("Error en findAll del LibroHome: " +
ex.getMessage());
}
return lista;
}
//metodos de busqueda de libros por criterios
public ArrayList<Libro> findCriteria(String isbn, String titulo,
String tema,String autor,double preio)
{
ArrayList<Libro>lista = null;
try{
lista = (ArrayList<Libro>)entityManager.createQuery("select libr
o from Libro as libro where eliminado=0 and titulo like :criterio1 and tema.nomb
re like :criterio2 and autor.nombre like :criterio3")
.setParameter("criterio1",'%' +titulo+ '%')
.setParameter("criterio2",'%' +tema+ '%')
.setParameter("criterio3",'%' +autor+ '%')
.getResultList();
}catch(RuntimeException ex){
System.out.print
ln("Error en findCriteria de libroHome"+ ex.getMessage());

}
return lista;
}

public ArrayList<Libro> findTema(int idTema){


ArrayList<Libro> lista =null;
try {
Query query= entityManager.createNativeQuery("call getLi
brostemas(?)",Libro.class).setParameter(1, idTema);
lista=(ArrayList<Libro>) query.getResultList();
} catch (RuntimeException ex) {
// TODO: handle exception
System.out.println("Error en findAll del LibroHome: " +
ex.getMessage());
}
return lista;
}
public double getPromedioprecio(){
double resultado=0;
try{
Query query = entityManager.createNativeQuery("call getP
romedioPrecio()");
resultado = (double)query.getSingleResult();
//OBTNEG
O SOLO UNA COLUMNA CON GETSINGLERESULT
}catch(RuntimeException ex){
log.error("Error en findAll getPromedioPrecio " + ex.get
Message());
}
return resultado;
}
}

Potrebbero piacerti anche