https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
In the reference, they only mention how to do nested projection with JPQL.
Assume I have these projection:
public interface ThreadWithContent {
Integer getId();
String getTitle();
UserSummary getAuthor();
}
public interface UserSummary {
Integer getId();
}
How can I query the Thread with projection using native query, I tried this:
@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);
But it looks like Spring Data can only map the thread
entity, but can't map the author
enitity
{
"title": "Recusandae nihil fugiat deserunt.",
"author": null,
"id": 5
}
I have tried author.id as authorId
, author.id as author_Id
but none of them works.