hibernatekotlinspring-data-jpahibernate-query

hibernate jpa projection with @Query


Most of the examples I've seen is using entityManager.createQuery or .createNativeQuery etc.

Is there a way to have something like the following working?

data class SummaryDto(val employeeName: String, val employerName: String)

@Query("select e.name as employeeName, emp.name as employerName " +
            "from Employer e " +
            "inner join Employee emp on emp.employer_id = e.id ", nativeQuery = true)
    fun findSummaries(): List<SummaryDto>

When I ran the above code

I got this error No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [dto.SummaryDto]

Can this be done with Kotlin or is there another way to get this to work with Hibernate JPA Annotation based?

Thanks Tin


Solution

  • For anyone that might run into the same problem. Change SummaryDto to an interface like this

    interface SummaryDto { val employeeName: String val employerName: String }

    will work.