I'm in a Java Desktop project using JPA, just to issue some reports where I'm using jasper. The reports have sql queries the user interface will only pass parameters to the 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 in Java Desktop is the first time I use reports, they are used by a web system and this is why 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 I get 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 am 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);
}
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 could I be wrong or if there is any other way to work with Jasper and JPA?