# Error mapping a query to a DTO

**URL:** <https://discourse.hibernate.org/t/error-mapping-a-query-to-a-dto/6782>\
**Category:** Hibernate ORM\
**Created:** [October 14, 2022, 10:21pm UTC](https://discourse.hibernate.org/t/error-mapping-a-query-to-a-dto/6782 "2022-10-14T22:21:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yabetancourt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yabetancourt/32/2002_2.png) [@yabetancourt](https://discourse.hibernate.org/u/yabetancourt)\
**Post date:** [October 14, 2022, 10:21pm UTC](https://discourse.hibernate.org/t/error-mapping-a-query-to-a-dto/6782/1 "2022-10-14T22:21:23Z")

</div>

hello i am using spring data jpa, hibernate and postgres. I have a query that filters and I want to save this result in a DTO.  
this is the entity I filter: Professor

```auto
@Entity
@NamedNativeQuery(name = "Professor.findByFullName",
        query = "select id, name, second_name, last_name from professor_filter(:term)",
        resultSetMapping = "Mapping.ProfessorDto"
)
@SqlResultSetMapping(name = "Mapping.ProfessorDto",
        classes = @ConstructorResult(targetClass = ProfessorDto.class,
                columns = {
                        @ColumnResult(name = "id", type = Long.class),
                        @ColumnResult(name = "name"),
                        @ColumnResult(name = "second_name"),
                        @ColumnResult(name = "last_name")
                }))
public class Professor {
    //attributes and methods
}

```

this is the DTO

```auto
/**
 * A DTO for the {@link Professor} entity
 */

public final class ProfessorDto implements Serializable {

    private final Long id;
    private final String name;
    private final String secondName;
    private final String lastName;

    public ProfessorDto(Long id, String name, String second_name, String last_name) {
        this.id = id;
        this.name = name;
        this.secondName = second_name;
        this.lastName = last_name;
    }
    //other methods
}

```

and this is the repository

```auto
public interface ProfessorRepository extends JpaRepository<Professor, Long> {
    @Query(countQuery = "select count(id) from professor_filter(:term)",
            nativeQuery = true)
    Page<ProfessorDto> findByFullName(@Param("term") String fullName, Pageable pageable);
}

```

the function professor\_filter

```auto
CREATE OR REPLACE FUNCTION public.professor_filter(text_filter text)
    RETURNS TABLE(id bigint, name character varying, second_name character varying, last_name character varying) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
select pr.id, name, second_name, last_name
from person p
         inner join professor pr on p.id = pr.person_id where concat(name, ' ', second_name, ' ', last_name) ilike concat('%',$1, '%'); 
$BODY$;

ALTER FUNCTION public.professor_filter(text)
    OWNER TO postgres;

```

When I try to run this I get the following error

```auto
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: professor_filter near line 1, column 46 [select id, name, second_name, last_name from professor_filter(:term)]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: professor_filter near line 1, column 46 [select id, name, second_name, last_name from professor_filter(:term)]

```

[see the complete logs](https://gist.github.com/yabetancourt/5160a7e5bf0bc86f45c47fb722c74fbc)

What is the reason for this error?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 17, 2022, 9:53am UTC](https://discourse.hibernate.org/t/error-mapping-a-query-to-a-dto/6782/2 "2022-10-17T09:53:34Z")

</div>

The exception implies that the count query is treated as HQL rather than a native query. Ask the Spring team what goes wrong in Spring Data there.
