Hi,
I am using Jasper Report with Hibernate and Spring env. Everything seems to work great excepts that when my hibernate query has a field of NUMBER type (Oracle), the report throws ClassCastException when evaluates the JRField.
Here is brief of what I have:
1. Query in the hibernate mapping file(hbm.xml file):
i.e SELECT a, b, c from myTable; (where a and c are VARCHAR2 and b is NUMBER. I named mySelect1).
2. Created a Class (myHQDS) to implement JRDataSource interface.
3. Code in the program:
List report = session.find("mySelect1"); Map parameters = new HashMap(); parameters.put("Title", "The Report"); ...Load and compile the report String[] fields = new String[] { "a", "b", "c"}; myHQDS ds = new myHQDS(report, fields); JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, ds); JasperManager.printReportToPdfFile(jasperPrint, "the-report.pdf");
4. Error encountered:
net.sf.jasperreports.engine.fill.JRExpressionEvaluationException: Error evaluation exception
Source text: $F{b}.
...
java.lang.ClassCastException: java.math.BigDecimal
at the-report_12.....evaluate(the-report_12......:201)
net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 186)
I changed the filed name in the mapping file as well as the java object from int, Integer to java.lang.Number ...etc, but still I have the error when evaluate this filed. If I commeded this field out, it works perfectly fine. Can some body help???
Thanks in advance,
Bob
14 Answers:
Lucian,
Thanks for your response. Yes I tried to change the data type from Number, int to BigDecimal...etc. But I still the ClassCastException error when evaluate field. Below is the simplyfied version of my JRXML. Thanks-
<?xml version="1.0"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="myTemplate">
<parameter name="TITLE" class="java.lang.String"/>
<field name="a" class="java.lang.String"/>
<field name="b" class="java.lang.Number"/>
<field name="c" class="java.lang.String"/>
<detail>
<band height="40">
<textField>
<reportElement x="0" y="0" width="150" height="40"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{a}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="150" y="0" width="150" height="40"/>
<textFieldExpression class="java.lang.Number">
<![CDATA[$F{b}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="300" y="0" width="150" height="40"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{c}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
My Jasper works is on a diff network so I can't cut and paste it here, but something like this:
net.sf.jasperreports.engine.fill.JRExpressionEvaluationException: Error evaluation exception
Source text: $F{b}.
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 197)
net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java: 574)
net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java: 542)
...
...
net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java: 368)
Truncated. See log file for complete Starcktrace
java.lang.ClassCastException: java.math.BigDecimal
at the-report_12.....evaluate(the-report_12......:201)
net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java: 186)
...
net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java: 835)
Truncated. See log file for complete Starcktrace
I ran a debugger and found the values for the 3 fields a, b, and c. The a and c fields values are as normal as String results, but the b field is returned as BigDecimal. Is it normal?
Thanks again for your help.
Bob
Could you set the net.sf.jasperreports.compiler.keep.java.file property to true, and check the Java file generated by JR for the report? Go to line 201 to see how the expression got translated in the file..
Regards,
Lucian
Sorry for the long pause.
You can tell the compiler where to keep the generated file via the net.sf.jasperreports.compiler.temp.dir property.
If seeing the generated Java file doesn't yield any clues, the only solution is to prepare a self-contained test case to reproduce the problem. Otherwise I'm afraid this investigation isn't going to have any finalization.
Regards,
Lucian
I found a work around for this issue by adding a check for the returned column in question and convert it to Integer...
i.e:
====
public Object getFieldValue(JRField field) throws JRException{
Object value = null;
int index = getFieldIndex(field.getName());
if (index !=-1){
//This block is added...
Object[] values = (Object[])currentValue;
if("columnInQuestion".equalsIgnoreCase(field.getName()))
{
Integer myInt = new Integer(values[index].toString());
value = myInt;
}else{
value = values[index];
}
}
return value;
}