bobz28 Posted January 13, 2010 Share Posted January 13, 2010 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 reportString[] 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");[/code]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 Link to comment Share on other sites More sharing options...
bobz28 Posted January 14, 2010 Author Share Posted January 14, 2010 Does anyone have any answer? Link to comment Share on other sites More sharing options...
lucianc Posted January 15, 2010 Share Posted January 15, 2010 Post your JRXML.If you're using $F{b} as expression for a text field, make sure you do <textFieldExpression class="java.lang.Number">$F{b}</textFieldExpression>Regards,Lucian Link to comment Share on other sites More sharing options...
bobz28 Posted January 15, 2010 Author Share Posted January 15, 2010 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> Link to comment Share on other sites More sharing options...
lucianc Posted January 19, 2010 Share Posted January 19, 2010 The JRXML looks fine, assuming that the data source returns a BigDecimal as value for the "b" field.Create a text field that shows $F{b}.getClass().getName() and see what the output is when you run the report.Regards,Lucian Link to comment Share on other sites More sharing options...
bobz28 Posted January 19, 2010 Author Share Posted January 19, 2010 Hi Lucian, How can I create a text field $F{b}.getClass().getName()? you mean in JRXML file?Because, as soon as I include the b field in my hibernate query and in my code, I get the error as I posted above. Thanks,Bob Link to comment Share on other sites More sharing options...
lucianc Posted January 20, 2010 Share Posted January 20, 2010 Where is this error coming from? Please post the full exception stacktrace. Link to comment Share on other sites More sharing options...
bobz28 Posted January 20, 2010 Author Share Posted January 20, 2010 Hi Lucian, 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 Starcktracejava.lang.ClassCastException: java.math.BigDecimalat 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 Link to comment Share on other sites More sharing options...
bobz28 Posted January 25, 2010 Author Share Posted January 25, 2010 Lucian,I ran the debug and got the value for "b" field is BigDecimal. I am expecting a numeric value instead...Do you have idea, suggestion? Thanks,Bob Link to comment Share on other sites More sharing options...
lucianc Posted January 29, 2010 Share Posted January 29, 2010 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 Link to comment Share on other sites More sharing options...
bobz28 Posted February 3, 2010 Author Share Posted February 3, 2010 Hi Lucian, I made the change and re-run...But don't know where to look for the generated Java file...Where should it be? Thanks,Bob Link to comment Share on other sites More sharing options...
bobz28 Posted February 17, 2010 Author Share Posted February 17, 2010 Can some body help??? I am stuck... Link to comment Share on other sites More sharing options...
lucianc Posted February 23, 2010 Share Posted February 23, 2010 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 Link to comment Share on other sites More sharing options...
bobz28 Posted February 25, 2010 Author Share Posted February 25, 2010 Hi Lucian,I am using Jaspperreports-3.1.0 and couldn't find the property file in net.sf.jasperreports.compiler.temp.dir directory. Thanks,Bob Link to comment Share on other sites More sharing options...
bobz28 Posted March 3, 2010 Author Share Posted March 3, 2010 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;} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now