使用@Query休眠jpa投影 [英] hibernate jpa projection with @Query
问题描述
我看到的大多数示例都是使用entityManager.createQuery或.createNativeQuery等.
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>
当我运行上面的代码时
我收到此错误
找不到能够从类型转换的转换器
[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [dto.SummaryDto]
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]
这可以用Kotlin完成吗,还是有另一种方法可以使它与基于Hibernate JPA Annotation一起使用?
Can this be done with Kotlin or is there another way to get this to work with Hibernate JPA Annotation based?
谢谢 锡
推荐答案
针对可能会遇到相同问题的任何人.将SummaryDto
更改为这样的界面
For anyone that might run into the same problem. Change SummaryDto
to an interface like this
interface SummaryDto {
val employeeName: String
val employerName: String
}
interface SummaryDto {
val employeeName: String
val employerName: String
}
将起作用.
这篇关于使用@Query休眠jpa投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!