[hibernate-dev] Making APIs more Lambda-friendly

Gunnar Morling gunnar at hibernate.org
Tue Mar 1 06:55:01 EST 2016


Hi,

Tinkering with result transformers, I wished I'd be able to express a
tuple transformation like this, using Java 8 Lambda-style:

    session.createQuery( "SELECT foo, bar FROM Baz" )
        .transformResultTuples( (tuple, aliases) -> { return tuple[0]
+ " " + tuple[1]; } )
        .list();

That requires an interface with a single abstract method ("functional
interface") describing the Lambda type. The current ResultTransformer
interface has two methods, so it cannot be used as is.

I thus propose to deprecate ResultTransformer and add two new,
separate contracts: ResultRowTransformer and ResultListTransformer.
Query#setResultTransformer() would be superseded by two new methods:
transformResultTuples() and transformResultList().

Note that this change is fully compatible with previous Java versions:
One still can pass an (anonymous) implementation of the contracts when
stuck to Java 6/7. That backwards-compatible design is a really nice
trait of how Lambdas have been added to the Java language.

Lambda-friendliness is something which I think we should generally
keep in mind when designing new APIs. Maybe there also other ones
existing, that'd benefit from retrofitting.

Any thoughts?

Thanks,

--Gunnar


More information about the hibernate-dev mailing list