Antoniy Milenkinski
Antoniy Milenkinski

Reputation: 35

Hibernate HQL - query.list() return Object array of object arrays

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.

Upvotes: 1

Views: 4559

Answers (1)

StanislavL
StanislavL

Reputation: 57421

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

Upvotes: 2

Related Questions