hibernate jpa projection with @Query

做~自己de王妃 提交于 2021-01-27 13:00:22

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/52149389/hibernate-jpa-projection-with-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!