https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
在参考文献中,他们只提到了如何使用JPQL进行嵌套投影。假设我有以下投影:
public interface ThreadWithContent {
Integer getId();
String getTitle();
UserSummary getAuthor();
}
public interface UserSummary {
Integer getId();
}
我该如何使用本地查询来查询线程并进行投影?我尝试了以下代码:
@Query(value =
"select thread.id as id,thread.title as title,author.id as authorId "+
"from thread inner join users as author " +
"on thread.author_id = author.id " +
"where thread.id = ?1",nativeQuery = true)
ThreadWithContent getThreadsById(Integer threadID);
但是看起来Spring Data只能映射thread
实体,而不能映射author
实体。
{
"title": "Recusandae nihil fugiat deserunt.",
"author": null,
"id": 5
}
我尝试了 author.id as authorId
, author.id as author_Id
,但它们都不能正常工作。