Jasper with JPA connection

1

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?

    
asked by anonymous 16.05.2018 / 04:40

1 answer

0

Gentlemen the solution to my problem was as follows First - there was an error in my jrxml I had put a Pwhere parameter of type string and my query for example was the following

Select <Lista dos campos> From <Tabela> Where 1 = 1 $P!{Pwhere}

As I was not passing this parameter it was null and at the time of executing the query there was an error which I resolved by giving a default for this (in case I did not send any by the application).

Second - about the connection since I'm using JPA with Hibernate I picked up a class that helped me in this issue on Armando Couto's Blog

class ReportsRepository {

   private EntityManager entityManager;

   public ReportsRepository(EntityManager entityManager) {
       this.entityManager = entityManager;
   }

   public Connection getConnection() {
       try {
           EntityManagerImpl factory = (EntityManagerImpl) entityManager;
           SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) factory.getSession().getSessionFactory();
           return sessionFactoryImpl.getConnectionProvider().getConnection();
       } catch (SQLException e) {
       }
       return null;
}
    
18.05.2018 / 14:36