我陷入了以下情况:
我的实体彼此相关,但是我无法使用 JPQL。我被迫使用本机 SQL。现在我想将这些结果映射到一个 ValueObject。需要明确的是,我不想获取 Object 数组 ( List<Object[]>
) 的列表。我有 6 个实体,我只需要其中的一些列。谁能给我一个关于如何从本机查询实现这种映射的示例?
我经历的教程。
我的代码:
@SqlResultSetMapping(
name = "findAllDataMapping",
classes = @ConstructorResult(
targetClass = MyVO.class,
columns = {
@ColumnResult(name = "userFirstName"),
@ColumnResult(name = "userLastName"),
@ColumnResult(name = "id"),
@ColumnResult(name = "packageName")
}
)
)
@NamedNativeQuery(name = "findAllDataMapping",
query = "SELECT " +
" u.first_name as userFirstName, " +
" u.last_name as userLastName, " +
" i.id as id, " +
" s.title as packageName, " +
"FROM " +
" invoice as i " +
"JOIN user as u on i.user_id=u.id " +
"LEFT JOIN subscription_package as s on i.subscription_package_id=s.id " +
"where u.param1=:param1 and i.param2=:param2" +
)
public class MyVO {
private String userFirstName;
private String userLastName;
private Long id;
private String packageName;
public MyVO (String userFName, String userLName,
Long id, String packageName) {
this.userFirstName = userFName;
this.userLastName = userLName;
this.id = id;
this.packageName = packageName;
}
// getters & setters
}
在我的 jpa-repository 模块中:
public interface MyRepository extends JpaRepository<MyEntity, Long> {
List<MyVO> findAllOfMyVO(@Param("param1") String param1, @Param("param2") String param2);
}
关键是我不知道将这些注释放在哪里,所以我可以使用这种映射。在本机查询中,我不能使用new rs.rado.leo.mypackage.MyVO(...)
. 我收到以下错误:
造成的:
org.springframework.data.mapping.PropertyReferenceException: No property findAllOfMyVO found for type MyEntity!
我想我的问题很清楚。如果没有,请告诉我,以便我可以编辑我的问题。
提前致谢!