blackdog
blackdog

Reputation: 2107

How to use `setResultTransformer` after Hibernate 5.2?

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?

Upvotes: 42

Views: 48966

Answers (4)

user5182503
user5182503

Reputation:

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

Upvotes: 4

Ranjeet Rana
Ranjeet Rana

Reputation: 65

Since Hibernate 5.2 uses Java 8 you can use stream api

List<EmployeeDto> resultList = session
            .createQuery(" from Employee", Employee.class)
            .stream()
            .map((employee) -> {
                return new EmployeeDto(employee);
            })
            .collect(Collectors.toList());

This code will also give some performance improvement.

Upvotes: -12

ali akbar azizkhani
ali akbar azizkhani

Reputation: 2279

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/

Upvotes: 14

HappyFace
HappyFace

Reputation: 4093

Don't use session.createQuery(hql,transformerClass); if you select multiple items in your query, use the old deprecated method instead.

Upvotes: -1

Related Questions