views:

68

answers:

1

Good afternoon, I'm trying to learn nHibernate and it's not terribly clear.

I need to get the result of an sql query:

select patient.name,
       discipline.description,
       sum(patient.enddate - patient.startdate) as totaltime
from treatment
 join patient on patient.id = treatment.patientId
 join discipline.id = treatment.disciplineId

I don't need to persist the result, just display it.

  • If I use hql directly:

What objects will it instantiate and return to me? Will it dynamically build a list of objects containing fields identical to the columns in the result set? The docs leave out this information.

  • If I need to make a mapping:

Do you create a mapping to a 'meta' object or to one of the joined tables (say 'treatment')?

Thanks

A: 

This can be achieved by using DTO objects ("data transfer objects"). You create an object that contains only the data you wish to return. Add an initializing constructor. Then your hql can be of the form:

select new mydto( x.value, y.value ) from x join x.y;

It doesn't seem to like using aggregate functions in the object constructor though.

Jay

related questions