# Mappers

# Get result as object list by NativeObjectMapper

//SELECT a.* FROM address
NativeCriteria nc = new NativeCriteria(new HibernateQueryProvider(session), "ADDRESS", "a")
                        .add(NativeExps.eq("a.city", "WARSAW"));
List<Address> res = nc.criteriaResult(cr -> {
    Address a = new Address();
    a.setId(cr.getLong(0));
    a.setCity(cr.getString(1));
    a.setStreet(cr.getString(2));
    a.setBuildingNumber(cr.getString(3));
    a.setZipCode(cr.getString(4));
    return a;
});
1
2
3
4
5
6
7
8
9
10
11
12
# TIP: You could always define the mapper as separate object:
NativeCriteria nc = createNativeCriteria("ADDRESS", "a").add(NativeExps.eq("a.city", "Warsaw"));
List<Address> res = nc.criteriaResult(Address.NATIVE_OBJECT_MAPPER);
1
2

# Get any result by CriteriaResultTransformer

NativeCriteria nc = new NativeCriteria(new HibernateQueryProvider(session), "ADDRESS", "a");
Multimap<String, String> zipCodesGroupedByCities = nc.criteriaResult(new CriteriaResultTransformer<Multimap<String,String>>() {
    @Override
    public Multimap<String, String> transform(CriteriaResult criteriaResult) {
        Multimap<String, String> result = HashMultimap.create();
        while (criteriaResult.next()) {
            // 1 - city code, 4 - zip code
            result.put(criteriaResult.getString(1), criteriaResult.getString(4));
        }
        return result;
    }
});
1
2
3
4
5
6
7
8
9
10
11
12

# See more mappers on Spring module