Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `setResultTransformer` after Hibernate 5.2?

Tags:

java

hibernate

I wanna use query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP) to get a List<Map>. But I got a exception:

java.lang.NoSuchMethodError: org.hibernate.query.Query.setResultTransformer(Lorg/hibernate/transform/ResultTransformer;)Lorg/hibernate/Query; 

I can't find the implemented class of org.hibernate.query.Query. The method setResultTransformer is in org.hibernate.Query.

And why is the org.hibernate.Query deprecated?

like image 385
blackdog Avatar asked Jul 07 '16 07:07

blackdog


People also ask

What is the use of setResultTransformer in hibernate?

Hibernate's ResultTransformers provide various ways to map the result of your query to different data structures. They were commonly used in Hibernate 4, got deprecated in Hibernate 5 and got replaced by the functional interfaces TupleTransformer and ResultListTransformer in Hibernate 6.

Why is setResultTransformer deprecated?

The ResultTransformer comes with a legacy definition which is not following the Functional Interface syntax. Hence, we cannot use a lambda in this example. Hibernate 6.0 aims to overcome this issue, so that's why the Hibernate ORM 5.2 ResultTransformer is deprecated.

What can I use instead of setResultTransformer?

Which method replaced setResultTransformer ? For the moment, there is no replacement. In 6.0, the ResultTransformer will be replaced by a @FunctionalInterface , but the underlying mechanism of transforming the Object[] property values will probably be the same.

What is result transformer?

public interface ResultTransformer extends Serializable. Implementors define a strategy for transforming query results into the actual application-visible query result list.


2 Answers

The ResultTransformer comes with a legacy definition which is not following the Functional Interface syntax. Hence, we cannot use a lambda in this example. Hibernate 6.0 aims to overcome this issue, so that’s why the Hibernate ORM 5.2 ResultTransformer is deprecated. Nevertheless, an alternative will be provided, so the concept we are discussing in this article is going to stand still even in Hibernate 6.

https://vladmihalcea.com/why-you-should-use-the-hibernate-resulttransformer-to-customize-result-set-mappings/

like image 180
ali akbar azizkhani Avatar answered Sep 17 '22 18:09

ali akbar azizkhani


Hibernate 6 has not been released yet. However, based on this issue ResultTransformer interface was split into the 2 functional interfaces: TupleTransformer and ResultListTransformer.

like image 27
Pavel_K Avatar answered Sep 19 '22 18:09

Pavel_K