@Query("SELECT tt, at.field, at.anotherField from TableTest tt LEFT JOIN AnotherTable at ON at.commonField = tt.commonField")
List<TestPojo> findAllPojo(List<TableTestDTO> TableTestDTOList);
如何在没有 native 查询的情况下将此 JPA 查询结果映射到 Pojo,例如 this approach ?
我正在使用 JPA 和 Hibernate。谁能提供其他选择吗?
最佳答案
尝试使用构造函数:
@Query("SELECT new TestPojo(tt, at.field, at.anotherField) from TableTest tt LEFT JOIN AnotherTable at ON at.commonField = tt.commonField")
List<TestPojo> findAllPojo(List<TableTestDTO> TableTestDTOList);
当然,这样的构造函数必须存在,更好的是放置完全限定名称而不是裸露的TestPojo
。
关于java - 如何将JPA查询结果映射到POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815003/