我遇到了以下情况:
我的实体之间存在关联,但是无法使用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!
我想我的问题已经很清楚了。如果不清楚,请告诉我,这样我就可以编辑我的问题。
提前感谢!