Skip to content

Selecting a collection into the dto

gert-wijns edited this page Sep 13, 2014 · 2 revisions

It is possible to select a collection into the result dto by

  • providing the identifier of the result dto, so elements of a collection may be added to the result dto with the same identifier. This can be done by providing the additional argument when selecting the result dto
  • creating a result dto for the collection items using select method with a collection and the required element type.
Town town = query.from(Town.class);
Person inhabitant = query.join(town.getInhabitants());

// only people with a name starting with 'G'
query.where(inhabitant.getName()).startsWith("G");

// select a townDto, and provide the identity field
TownDto townDto = query.select(TownDto.class, 
        new IdentityFieldProvider<TownDto>() {
    @Override
    protected Object getIdentifier(TownDto resultProxy) {
        return resultProxy.getId();
    }
});
// set some townDto fields
bindTownDto(townDto, town);

// create a personDto to select inhabitants into
// the town dto's inhabitants
PersonDto personDto = query.select(
        townDto.getInhabitants(), 
        PersonDto.class, null);
// set some personDto fields
bindPersonDto(personDto, inhabitant);

=> "select hobj1.id as id, 
           hobj2.id as g1__id, 
           hobj2.age as g1__personAge, 
           hobj2.name as g1__thePersonsName 
    from Town hobj1 
    join hobj1.inhabitants hobj2 
    where hobj2.name like :np1"
params: [np1="G%"]
Clone this wiki locally