[hibernate-dev] Making APIs more Lambda-friendly

Gunnar Morling gunnar at hibernate.org
Wed Mar 2 02:46:12 EST 2016


ResultTransformer wouldn't even be needed anymore eventually. There
could be an adapter class of sorts internally which aggregates both
methods if that simplifies handling internally, but it's not needed
per se.

2016-03-01 13:37 GMT+01:00 Vlad Mihalcea <mihalcea.vlad at gmail.com>:
> Good idea.
>
> So, ResultTranformer will extend both the ResultRowTransformer and
> ResultListTransformer so
> it will inherit both methods.
>
> Actually, the `transformList(List list)` is only needed by
> `DistinctResultTransformer`, so most of the time we'd only have to pass
> `ResultRowTransformer`.
>
> Vlad
>
> On Tue, Mar 1, 2016 at 1:55 PM, Gunnar Morling <gunnar at hibernate.org> wrote:
>>
>> 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
>> _______________________________________________
>> hibernate-dev mailing list
>> hibernate-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/hibernate-dev
>
>


More information about the hibernate-dev mailing list