Skip to content

Result Handler

The result handler customizes the conversion logic of field values when mapping from a database ResultSet to Java / Kotlin objects. You can specify handlers via global configuration or field annotations.

Lookup Priority

When query results are mapped to entity fields, Kite looks up a result handler in the following priority:

  1. Field Annotation — handler specified by @Column(resultHandler = ...), highest priority
  2. Global Configuration — type handlers registered in KiteConfig.resultHandlers
  3. Built-in Defaults — Kite's built-in handlers for common types

Built-in Handlers

Kite provides default result handlers for the following types, ready to use without any configuration:

PackageTypeHandler
primitiveStringStringResultHandler
primitiveCharCharResultHandler
primitiveShortShortResultHandler
primitiveByteByteResultHandler
primitiveIntIntResultHandler
primitiveLongLongResultHandler
primitiveDoubleDoubleResultHandler
primitiveFloatFloatResultHandler
primitiveBooleanBooleanResultHandler
mathBigDecimalBigDecimalResultHandler
mathBigIntegerBigIntegerResultHandler
timejava.sql.DateSqlDateResultHandler
timejava.sql.TimeTimeResultHandler
timejava.sql.TimestampTimestampResultHandler
timejava.util.DateDateResultHandler
timejava.util.CalendarCalendarResultHandler
timejava.time.InstantInstantResultHandler
timejava.time.LocalDateLocalDateResultHandler
timejava.time.LocalTimeLocalTimeResultHandler
timejava.time.LocalDateTimeLocalDateTimeResultHandler

DefaultResultHandler is used when no handler matches the field type.

Global Configuration

Register custom handlers via KiteConfig.resultHandlers. Global configuration overrides built-in defaults.

java
import com.tang.kite.config.KiteConfig;
import com.tang.kite.handler.result.ResultHandler;

KiteConfig.getResultHandlers().put(String.class, new TrimmedStringResultHandler());

Field Annotation

Use the resultHandler attribute of the @Column annotation to specify a handler for a single field, which takes precedence over global configuration.

java
import com.tang.kite.annotation.Column;

public class Account {

    @Column(resultHandler = TrimmedStringResultHandler.class)
    private String name;

}

Custom Handlers

Implement the ResultHandler interface to create a custom result handler. The core method is setValue. When the value is null, setNullValue is called (default implementation is Reflects.setValue(field, instance, null), override as needed).

It is recommended to use Reflects.setValue for setting field values — it automatically handles visibility without the need to explicitly call trySetAccessible.

java
import com.tang.kite.exception.UnsupportedTypeException;
import com.tang.kite.handler.result.ResultHandler;
import com.tang.kite.utils.Reflects;
import org.jspecify.annotations.NonNull;
import java.lang.reflect.Field;

public class TrimmedStringResultHandler implements ResultHandler {

    @Override
    public <T> void setValue(@NonNull Field field, T instance, @NonNull Object value) {
        String result;
        if (value instanceof String string) {
            result = string.trim();
        } else if (value instanceof Number number) {
            result = String.valueOf(number);
        } else {
            throw new UnsupportedTypeException(value.getClass(), field);
        }
        Reflects.setValue(field, instance, result);
    }

}

Released under the MIT License