Skip to content

Selecting the relevant data

gert-wijns edited this page May 21, 2014 · 7 revisions

Selecting specific data into dtos

To select the queried results into dtos, first call the select(Class<?> dtoClass) on the TypeSafeRootQuery. Doing this will provide a proxy instance of the dtoClass. This proxy will capture the invoked setters and bind the set data to the alias of the setter. When the query is executed, use the ResultTransformer available on the HqlQuery obtained by calling query.toHqlQuery() to automatically convert the tuples into instances.

The set values can be entity properties, subquery values or function values.

Person person = query.from(Person.class);
PersonDto personDto = query.select(PersonDto.class); // proxy instance of dto class
personDto.setPersonAge(person.getAge());
personDto.setThePersonsName(person.getName()); // selects the name into thePersonName of personDtos

hqlQuery.getHql() would yield => 
"select hobj1.age as personAge, hobj1.name as thePersonsName from Person hobj1"

Selecting values without using dtos

When simply querying single values, it would be inconvenient to have to make use of the dto class selection method. For this case, and when it is not desired to select into a dto, it is possible to call selectValue(Object obj) on the TypeSafeRootQuery. This method may be called as many times as desired and will generate a select statement without aliases.

Person person = query.from(Person.class);
query.selectValue(person.getName());
query.selectValue(person.isMarried());

hqlQuery.getHql() would yield => 
"select hobj1.name, hobj1.married from Person hobj1"

More advanced information about selecting can be found at Advanced selection binding.

Next: Grouping data

Clone this wiki locally