我想执行一个@NamedNativeQuery。我正在该查询中执行各种类型的操作。由于某些原因,我在没有任何特定原因的情况下在命名查询中遇到错误。我有一个SurveyResponse类,如下:
@NamedNativeQuery( name = "fetchSurveyResponse",
query = "select query_id, query," +
" sum(case when response = 'BothGood' then count else 0 end) as
both_good," +
" sum(case when response = 'BothBad' then count else 0 end) as
both_bad," +
" from" +
" (" +
" select q.query_id, query, response, COUNT(response) AS 'count'" +
" from query q" +
" join survey_response sr" +
" on (q.query_id = sr.query_id)" +
" where sr.survey_id = ?1" +
" Group by q.query_id, query, response" +
" Order by query" +
" ) AS new_table" +
" group by query_id, query", resultClass=SurveyResult.class)
@Entity
@Table(name = "survey_response")
public class SurveyResponse {
@Id
private int surveyResponseId;
private int userId;
private int queryId;
private int surveyId;
private String response;
public int getSurveyResponseId() {
return surveyResponseId;
}
public void setSurveyResponseId(int surveyResponseId) {
this.surveyResponseId = surveyResponseId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getQueryId() {
return queryId;
}
public void setQueryId(int queryId) {
this.queryId = queryId;
}
public int getSurveyId() {
return surveyId;
}
public void setSurveyId(int surveyId) {
this.surveyId = surveyId;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
我的SurveyResult类如下:
public class SurveyResult {
private int queryId;
private String query;
private int bothGood;
private int bothBad;
public int getQueryId() {
return queryId;
}
public void setQueryId(int queryId) {
this.queryId = queryId;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public int getBothGood() {
return bothGood;
}
public void setBothGood(int bothGood) {
this.bothGood = bothGood;
}
public int getBothBad() {
return bothBad;
}
public void setBothBad(int bothBad) {
this.bothBad = bothBad;
}
}
我的存储库类如下:
@Repository
public interface SurveyResponseRepository
extends JpaRepository<SurveyResponse, Long>, JpaSpecificationExecutor<SurveyResponse> {
@Query(name="fetchSurveyResponse")
public List<SurveyResponse> getSurveyResponses(int surveyId);
}
有人可以帮助我解决此错误吗?谢谢。