-
-
Save gitdealdo/6df282d2fdb9dfa44b4165e997c5e8b6 to your computer and use it in GitHub Desktop.
Acceso al objecto Connection a través de Hibernate 4 para utilizarla como Datasource en Jasperreports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityManager; | |
import org.hibernate.Session; | |
import org.hibernate.ejb.HibernateEntityManager; | |
import org.hibernate.jdbc.Work; | |
import net.sf.jasperreports.engine.JRException; | |
import net.sf.jasperreports.engine.JasperCompileManager; | |
import net.sf.jasperreports.engine.JasperFillManager; | |
import net.sf.jasperreports.engine.JasperPrint; | |
import net.sf.jasperreports.engine.JasperReport; | |
// ... | |
try{ | |
// Obtenemos el entityManager de JPA. En este caso viene inyectado | |
HibernateEntityManager em = (HibernateEntityManager)entityManagerProvider.get(); | |
Session session = em.getSession(); | |
session.doWork(new Work() | |
{ | |
@Override | |
public void execute(Connection connection) throws SQLException | |
{ | |
try{ | |
InputStream fileStream = getClass().getResourceAsStream(reportPath); | |
JasperReport compiledReport = JasperCompileManager.compileReport(fileStream); | |
JasperPrint jasperPrint = JasperFillManager.fillReport(compiledReport, reportParameters, connection); | |
// Visualizar o guardar el informe resultante | |
} | |
catch(JRException e){ | |
throw new RuntimeException(e); | |
} | |
} | |
}); | |
} | |
catch(Exception e){ | |
// Tratar el error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment