使用@Query的Hibernate JPA投影

4

我看到的大多数示例都使用entityManager.createQuery或.createNativeQuery等方法。

有没有一种方法可以使以下内容正常工作?

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]转换为类型[dto.SummaryDto]

这是否可以使用Kotlin完成,或者还有其他方法可以在Hibernate JPA注释中实现此功能?

谢谢 Tin

1个回答

7

对于可能遇到相同问题的人。将SummaryDto更改为以下接口即可:

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

这样就可以解决问题了。


但为什么它有效? - Wood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接