Skip to content

Miscellaneous

Miscellaneous features

Explicit instantiators

Normally Dalesbred will automatically detect the best way to instantiate your classes based on database results. This can sometimes lead to surprising results. If you wish to be more explicit, you can annotate your preferred constructor with DalesbredInstantiator. This will cause Dalesbred to ignore all other constructors.

Large objects

You can stream large objects (blobs and clobs) to the database by just passing InputStream or Reader to a query. Similarly you can read them by asking back for InputStream or Reader.

try (InputStream in = new FileInputStream(name)) {
    db.update("insert into my_file (name, contents) values (?,?)", name, in);
}

try (InputStream in = db.findUnique(InputStream.class,
                        "select contents from my_file where name=?", name)) {
    ...
}

Warning

The returned InputStream or Reader is only valid for the duration of the active transaction.

Custom type-conversions

Sometimes you need to convert database values to your own custom types and vice versa. To do that, you can register your functions to TypeConversionRegistry:

TypeConversionRegistry conversions = db.getTypeConversionRegistry();

// register conversions from database and to database types separately
conversions.registerConversionFromDatabase(
    String.class, EmailAddress.class, MyConversions::stringToEmail);
conversions.registerConversionToDatabase(
    EmailAddress.class, String.class, MyConversions::emailToString);

// or register both conversions with one call
conversions.registerConversions(
    String.class, EmailAddress.class, MyConversions::stringToEmail, MyConversions::emailToString);