2006 JR Open Discussion Posted August 22, 2006 Share Posted August 22, 2006 By: Yemelianov - yemelianov Getting ClassCastException when using count() 2006-03-20 13:09Hello, I am new to SourceForge. I need some help. The situation is simple, but I really do not understand why am I getting java.lang.ClassCastException: java.lang.Integer when trying to create a report. I use the latest version of JasperReports and Hibernate 3. I map my class to a table in my database as follows: <!------------------------------------------!> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.dao.hibernate.mapping.HibernateEntity" table="DMSEntity"> <composite-id class="com.dao.hibernate.mapping.EntityKey" name="key"> <key-property name="dmsCatalogId" column="dmsCatalogId" type="int"/> <key-property name="dmsEntityId" column="dmsEntityId" type="int"/> </composite-id> <many-to-one name="entityType" column="dmsEntityTypeId" class="com.dao.hibernate.mapping.HibernateEntityType"/> <many-to-one name="catalog" column="dmsCatalogId" class="com.dao.hibernate.mapping.HibernateCatalog" insert="false" update="false"/> <property name="profiles" column="Profiles"/> <property name="reference" column="Reference"/> <property name="name"/> <property name="creator"/> <property name="owner"/> <property name="lastModifiedBy"/> <property name="created"/> <property name="archiveTime"/> <property name="status"/> <property name="description"/> <property name="reviseComment"/> <property name="mimeType"/> <property name="versionNumber"/> <property name="revisionType"/> <property name="fileName"/> <property name="fileSize"/> ................. <!------------------------------------------!> My report definition defines <queryString language="hql"><![CDATA[select count(distinct he.entityType) as totalDocs from HibernateEntity he where he.owner=$P{owner} and he.created>'01-jan-2006' group by he.owner]]></queryString> <field name="totalDocs" class="java.lang.String"/> ......... <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement mode="Transparent" x="260" y="4" width="100" height="18" forecolor="#000000" backcolor="#FFFFFF" key="textField" stretchType="NoStretch" positionType="FixRelativeToTop" isPrintRepeatedValues="true" isRemoveLineWhenBlank="false" isPrintInFirstWholeBand="false" isPrintWhenDetailOverflows="false"/> <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/> <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single"> <font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" /> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[new String($F{totalDocs})]]></textFieldExpression> </textField> ............ when i execute JasperPrint jp = JasperFillManager.fillReport(report,parameters); logger.debug(CLASS_NAME,"Report begins"); JasperExportManager.exportReportToPdfFile(jp,application.getRealPath("/")+"/jsp/jasperreports/demo1.pdf"); I am getting the error: java.lang.ClassCastException: java.lang.Integer at net.sf.jasperreports.engine.data.JRHibernateAbstractDataSource$IndexFieldReader.getFieldValue(JRHibernateAbstractDataSource.java:276) at net.sf.jasperreports.engine.data.JRHibernateAbstractDataSource.getFieldValue(JRHibernateAbstractDataSource.java:236) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:727) at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:932) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:126) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:730) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:123) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:420) I gues there is something wrong with my query, but I cant figure it out? Can you give me some advices? Or may be I may use some variable to have the same result as my original HQL query does. Once again, the query is defined as select count(distinct he.entityType) as totalDocs from HibernateEntity he where he.owner=$P{owner} and he.created>'01-jan-2006' group by he.owner Sincerely. By: Lucian Chirita - lucianc RE: Getting ClassCastException when using cou 2006-03-21 08:50Hello The problem you faced was due to a bug in the code. A fix has been commited on CVS (look for JRHibernateAbstractDataSource.java rev 1.3). As a workaround (if you don't want to patch the JR code and rebuild it), you can change your query to select another dummy expression (which you can otherwise ignore): select count(distinct he.entityType) as totalDocs, 0 as dummy from HibernateEntity he ... Thanks, Lucian By: Lucian Chirita - lucianc RE: Getting ClassCastException when using cou 2006-03-21 09:12Note that you will also need to change the type of the totalDocs field to something compatible with java.lang.Integer, as the query returns an integer value for count(...) Regards, Lucian By: Yemelianov - yemelianov RE:issue with agregate count() for hibernat3 2006-03-21 11:55Thank's a lot for the fix. The proplem is solved. I actually recompiled JR with newest JRHibernateAbstractDataSource.java. Regars. 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