JasperReports for grails: Using HashMap as Model?

364 Views Asked by At

Imagine I have two classes in Groovy that look like that:

class Person {
  int id;
  String name;
}

class Item {
  int id;
  int price;
}

Now it would be simple to create a JasperReport listing all persons' names using the following SQL:

SELECT name FROM Person

Also, it would be easy to pass a Model from which the list should be created:

def p = Person.withCriteria {
   eq('name','SomeGuy')
}
chain(controller:'jasper',action:'index',model:[data:p],params:params)

But what I want to do is to use the following query:

SELECT name, (SELECT sum(i.price) FROM Item f WHERE f.id=p.id) as priceSum FROM Person p

And now this is the part where I don't know how to go on: How can I pass any Model to the jasperReport? I can't just use

def p = Person.withCriteria {
   eq('name','SomeGuy')
}

because then, the priceSum attribute would be missing. What I would like to do is something like this:

def l = ['name':'SomeGuy','priceSum':'5000']
chain(controller:'jasper',action:'index',model:[data:l],params:params)

But this doesn't work either, it gives me:

 Message: Cannot get property 'name' on null object 
 Caused by: java.lang.NullPointerException: Cannot get property 'name' on null object 

Is there anything simliar to this that would work?

0

There are 0 best solutions below