Jasper with JPA connection

Asked

Viewed 460 times

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?

1 answer

0

Gentlemen the solution to my problem was this: First - there was an error in my jrxml I had put a Pwhere parameter of string type 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 giving a default for this ( for the case I did not send any by the application ).

Second - about the connection since I am using JPA with Hibernate I took a class that helped me in this issue in 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.