'The result of a jdbc query is not readable for my jasper (report dependency), but with JPA is okay
SO: Debian 11
SDK: open-17 64 bit
Spring-boot-starter-data-jdbc:2.6.4
Spring-jdbc:5.3.16
HikariCP:4.0.3
Postgres 14
Hi. I try to make some pdf reports with Jasper Dependency inside of my Spring Boot project. Recently I was working with JPA, but I need to move to JDBC. I made a small report with JPA-Jasper, but try to do the same thing with JDBC-Jasper, and I have a problem with that, the type of data structure that jdbc.query is returning.
When I sout the result of the query db with JPA and JDBC respectively, I see a result like this structure: JPA ---> [{}, {}, ... {}] JDBC ---> [[], [], ... []]
Log example JPA
[Transaccion{id=1, transaccion_tipo_id=2, transaccion_estado_id=1, usuario_id=18, monto=0.0, referencia=19-0100-9, codigo_verificacion=0}, ...]
JDBC
[Transaccion[id=1, transaccion_tipo_id=2, transaccion_estado_id=1, usuario_id=18, monto=0.0, referencia=19-0100-9, codigo_verificacion=0], ...]
Here I have my data access service, using jdbc.query
@Override
public List<Transaccion> selectTransacciones() {
var sql = """
SELECT id,
transaccion_tipo_id,
transaccion_estado_id,
usuario_id,
monto,
referencia,
codigo_verificacion
FROM procesadora.transaccion
LIMIT 200
""";
return jdbcTemplate.query(sql, new TransaccionRowMapper());
};
This is what my rawMapper is returning
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TransaccionRowMapper implements RowMapper<Transaccion> {
@Override
public Transaccion mapRow(ResultSet resultSet, int rowNum) throws SQLException {
return new Transaccion(
resultSet.getLong("id"),
resultSet.getInt("transaccion_tipo_id"),
resultSet.getInt("transaccion_estado_id"),
resultSet.getInt("usuario_id"),
resultSet.getDouble("monto"),
resultSet.getString("referencia"),
resultSet.getString("codigo_verificacion")
);
}
}
Here is the transaccion Object, I use record to be make more cleaner.
public record Transaccion (Long id,
Integer transaccion_tipo_id,
Integer transaccion_estado_id,
Integer usuario_id,
Double monto,
String referencia,
String codigo_verificacion) {
}
Here I create the pdf Report
@Service
public class TransaccionReporte {
private final TransaccionService transaccionService;
public TransaccionReporte(TransaccionService transaccionService) {
this.transaccionService = transaccionService;
}
public String exportReport() throws FileNotFoundException, JRException {
System.out.println("*** reporte begins ***");
// bring data from db
List<Transaccion> transaccions = transaccionService.getTransacciones();
System.out.println(transaccions); // <--- here I expect [{}, {}] not [[], []]
System.out.println(transaccions.getClass());
System.out.println("*** List transacciones done ***");
// load file - blueprint for the pdf
File file = ResourceUtils.getFile("classpath:transaccion.jrxml");
System.out.println("*** Load file done ***");
// compile it
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(transaccionService.getTransacciones());
...
I think that is not a difficult problem, but a need more information about rowMapper, jdbc and all stuff releted to that, I anybody can give me a clue I'll very appreciated.
This is the repo I working on: https://github.com/biagiola/spring-jdbc-jasper-report
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
