Type specified for TypedQuery [com.softtrends.college.model.Student] is incompatible with query return type [class java.lang.String]; nested exception is java.lang.IllegalArgumentException: Type specified for TypedQuery [com.softtrends.college.model.Student] is incompatible with query return type [class java.lang.String]
这是控制器编码
//http://localhost:8080/college/subjectmaps/find?college=GCOEJ&subject=science&professor=R.S.Agrawal
@RequestMapping(value="/find", method = RequestMethod.GET)
public String findCollegeForCity(Model uiModel, @RequestParam(value = "college", required = true) String pCollege,
@RequestParam(value = "subject", required = false) String pSubject,
@RequestParam(value = "professor", required = false) String pProfessor,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "size", required = false) Integer size) {
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
uiModel.addAttribute("Subjectmaps", Subjectmap.listStudents(pCollege, pSubject, pProfessor, firstResult, sizeNo));
float nrOfPages = (float) Subjectmap.countStudents(pCollege, pSubject, pProfessor) / sizeNo;
uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
} else {
uiModel.addAttribute("Subjectmaps", Subjectmap.listStudents(pCollege, pSubject, pProfessor));
}
return "Subjectmaps/list";
}
the following queries i wrote in model
//list students studying a subject in a college under a professor.(college is mandatory)
public static List<Student> listStudents(String pCollege, String pSubject, String pProfessor) {
return entityManager().createQuery("SELECT o.studentId.studentName from Subjectmap o WHERE o.studentId.collegeId.collegeName=?1 AND (?2 IS null or o.subjectId.subjectName=?2) AND (?3 IS null OR o.professorId.professorName=?3)", Student.class)
.setParameter(1, pCollege)
.setParameter(2, pSubject)
.setParameter(3, pProfessor)
.getResultList();
}
public static long countStudents(String pCollege, String pSubject, String pProfessor) {
return entityManager().createQuery("SELECT o.studentId.studentName from Subjectmap o WHERE o.studentId.collegeId.collegeName=?1 AND (?2 IS null or o.subjectId.subjectName=?2) AND (?3 IS null OR o.professorId.professorName=?3)", Long.class)
.setParameter(1, pCollege)
.setParameter(2, pSubject)
.setParameter(3, pProfessor)
.getSingleResult();
}
public static List<Student> listStudents(String pCollege, String pSubject, String pProfessor, int pFirstResults, int pMaxResults) {
return entityManager().createQuery("SELECT o.studentId.studentName from Subjectmap o WHERE o.studentId.collegeId.collegeName=?1 AND (?2 IS null or o.subjectId.subjectName=?2) AND (?3 IS null OR o.professorId.professorName=?3)", Student.class)
.setParameter(1, pCollege)
.setParameter(2, pSubject)
.setParameter(3, pProfessor)
.setFirstResult(pFirstResults)
.setMaxResults(pMaxResults)
.getResultList();
}
答案 0 :(得分:0)
listStudents()
中的查询错误:您选择学生的姓名,但期望学生:SELECT o.studentId.studentName ...
也许这会奏效:(删除“.studentName”)
return entityManager().createQuery("SELECT o.studentId from Subjectmap o WHERE o.studentId.collegeId.collegeName=?1 AND (?2 IS null or o.subjectId.subjectName=?2) AND (?3 IS null OR o.professorId.professorName=?3)", Student.class)
如果没有,你可能不得不加入学生的桌子。