Home > Software engineering >  Cannot create TypedQuery for query with more than one return using requested result type, despite ha
Cannot create TypedQuery for query with more than one return using requested result type, despite ha

Time:11-13

I have gone through other similar questions on this topic and found that my setup is correct(at least confirms to what others had suggested in the past). I do have a dedicated AllRecordsDTO for the purpose of this query and it does have all the necessary fields for successful mapping of the query result.

AllRecordsDTO.java :

@NoArgsConstructor
@AllArgsConstructor
@ToString
public class AllRecordsDTO {

    @Getter
    @Setter
    private String recordName;

    @Getter
    @Setter
    private String recordTypeName;

    @Getter
    @Setter
    private String statusName;

    @Getter
    @Setter
    private String createdByUserId;

    @Getter
    @Setter
    private String createdDateTime;

    @Getter
    @Setter
    private String lastChangedByUserId;

    @Getter
    @Setter
    private String lastChangedDateTime;

}   

The allRecordsQuery query

    select r.recordName,
       rt.recordTypeName,
       s.name,
       usi.userId,
       r.createdDateTime,
       usi1.userId,
       r.lastChangedDateTime
from Record r
         left join Section s on s.id = r.statusId
         left join UniqueSecurityIdentifier usi on usi.id = r.createdByUserId
         left join RecordType rt on rt.id = r.recordTypeId
         left join UniqueSecurityIdentifier usi1 on usi1.id = r.lastChangedByUserId

The line that throws the exception :

Query<AllRecordsDTO> createdQuery = statelessSession.createQuery(allRecordsQuery, AllRecordssDTO.class);

All the Entity fields used in the query above are String(even the date fields are String in the above entities).

I am not sure what could have gone wrong.

CodePudding user response:

What you are trying is not possible with JPA.

You habe to use the Constructor Expression.

select NEW <packagename>.AllRecordsDTO(r.recordName,
       rt.recordTypeName,
       s.name,
       usi.userId,
       r.createdDateTime,
       usi1.userId,
       r.lastChangedDateTime)
from Record r
         left join Section s on s.id = r.statusId
         left join UniqueSecurityIdentifier usi on usi.id = r.createdByUserId
         left join RecordType rt on rt.id = r.recordTypeId
         left join UniqueSecurityIdentifier usi1 on usi1.id = r.lastChangedByUserId

Please also checkout the Hibernate documentation: https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause

  • Related