1

I'm writing a HQL query. Depending on conditions in my view I'm adding selects in SELECT clause(the number of selects is from 1 to about 20). Using list() on query hibernate will return Object array of object arrays at least I think so. If I iterate trough the result

 Iterator<Object[]> itr = result.iterator();
            while(itr.hasNext()){
               Object[] obj = (Object[]) itr.next();
               System.out.println(obj);                        //1st
               for (int br = 0; br <= obj.length-1;br ++) {
                   System.out.print(obj[br].toString() + ","); //2nd
               }
            }

The 1st Sytsem.out prints each Object array and the 2nd one prints their's content. For testing output to console is fine.

I try to use the results to Jasper Reports and somehow can't access the content of each Object array. Can someone give me advice or suggest an approach like converting the results to format I can use in report.

Bilal Shah
  • 1,135
  • 7
  • 17
  • 42
  • Put your `Jasper Report` code here. – Bilal Shah Nov 30 '15 at 06:58
  • `JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(result); JasperDesign jasperDesign = JRXmlLoader.load(pathToReport); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, beanColDataSource);` – Antoniy Milenkinski Nov 30 '15 at 07:20

1 Answers1

2

JRBeanCollectionDataSource supposes collection of beans - POJO classes. Then it tries to find proper field of the class by Jasper report's field name.

Thus if in Jasper Report your define a field 'userName' the JRBeanCollectionDataSource gets POJO class and tries to find the class' field 'userName'.

In your case instead of POJO class you get array Object[]. So can't find the field called 'userName'.

I suggest to define DTO (Data transfer object) and convert the Object[] to the DTO. Thus you will have List and the JRBeanCollectionDataSource can resolve proper names.

A simple way is to define results transformer.

.setResultTransformer( Transformers.aliasToBean(ResultsDTO.class))

See also this

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Yes, this will be the usual way I approach this, but here because my `SELECT` clause change depending on conditions, I do not know the number of fields I need to declare in the DTO. – Antoniy Milenkinski Nov 30 '15 at 07:49
  • You can use JRMapCollectionDataSource (list of maps). Just create a way to convert the Object[] to Map where String is the key must correspond the jasper report field name. – StanislavL Nov 30 '15 at 07:53
  • If I do it this way the number of fields in jasper report will be equal to number of objects my query return. My query will return variable number of objects so I don't know how to generate fields in jasper dynamical. Or I'm wrong – Antoniy Milenkinski Nov 30 '15 at 08:01
  • On my research of the problem found suggestions using CG lib or Javassist but it's a approach I've never used before. – Antoniy Milenkinski Nov 30 '15 at 08:03
  • Then I don't understand the problem. Your jasper reprot has some fixed fields (fixed names). You somehow should get the collection of field names and you have to create list of maps (one map for each row. Each the map should have as many key/value pairs as field names obtained from jasper. – StanislavL Nov 30 '15 at 08:10