1
I’m in a Java Desktop project using JPA, just to send a few reports where I’m using Jasper. Reports have sql queries the user interface will only pass parameters for data filtering.
I found it difficult to get the connection so in a quick search I found the code below
Session session = this.entityManager.unwrap(Session.class);
I would like to point out that using JPA and on Java Desktop is the first time I make use of reports, the same are of use of a Web system and this and for this reason I need to keep the report the same way to be used in both. So I wrote the class that generates the report, I did not succeed and the error message that I receive is the following
[AWT-EventQueue-0] WARN org.hibernate.util.JDBCExceptionReporter - SQL
Error: 0, SQLState: null
8780 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter -
Erro ao executar relatório /relatorios/RelFR_Usuario.jasper
Exception in thread "AWT-EventQueue-0"
org.hibernate.exception.GenericJDBCException: error executing work
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.impl.SessionImpl.doWork(SessionImpl.java:2001)
at br.com.dominio.view.FrmReportUsuario.imprime(FrmReportUsuario.java:122)
The Class I’m using as a report generator follows below
public class GeradorRelatorio implements Work {
private String caminhoRelatorio;
private Map<String, Object> parametros;
private boolean relatorioGerado;
public GeradorRelatorio(String caminhoRelatorio, Map<String, Object> parametros) {
this.caminhoRelatorio = caminhoRelatorio;
this.parametros = parametros;
this.parametros.put(JRParameter.REPORT_LOCALE, new Locale("pt", "BR"));
}
@Override
public void execute(Connection conn) throws SQLException {
try {
InputStream relatorioStream = this.getClass().getResourceAsStream(this.caminhoRelatorio);
JasperPrint print = JasperFillManager.fillReport(relatorioStream, this.parametros, conn);
JasperPrintManager.printReport(print, false);
} catch (Exception e) {
throw new SQLException("Erro ao executar relatório " + this.caminhoRelatorio, e);
}
}
and here’s where I’m calling
class FrmReportUsuario extends javax.swing.JInternalFrame {
private EntityManager entityManager;
private void imprime() {
Map parametros = new HashMap();
//parametros.put("CONTEUDO", "");
GeradorRelatorio gerador = new GeradorRelatorio("/relatorios/RelFR_Usuario.jasper",
parametros);
Session session = this.entityManager.unwrap(Session.class);
if (session.isConnected()){
System.out.println("Conectado");
} else {
System.out.println("Não Conectado");
}
session.doWork(gerador);
}
It also follows persistence.xml
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
Where I could be missing or if there is some other way to work with Jasper and JPA?