java - 如何将JPA查询结果映射到POJO?

标签 java spring spring-data-jpa pojo

@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/

相关文章:

java - jogl java.lang.UnsatisfiedLinkError : with gluegen-rt. dll

java - Paths.get ("")、Paths.get (".") 和新文件 (".") 之间的区别

javascript - 避免在 Spring 中重新加载 JSP 脚本文件

java - 带有集合参数的 Spring-Data @Query

java - 用户从不同国家登录时需要显示本地时间的交易。

java |验证包装器内的 JSON 对象

java - 数据库连接正在关闭

java - 在查询注释中更新时间戳字段 Spring JPA

java - 使用 Spring Data 和 Hibernate 的 Spring MVC 分页

Java 客户端-服务器 - 将文件分发到多个服务器