Hibernate + Spring Boot
我无法获得对象
我的控制器
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public @ResponseBody
SuperStudentEntity getById(@PathVariable("id") int id) {
return new StudentDAOImpl().getById(id);
}
DAO功能
public SuperStudentEntity getById(int id){
openTransactionSession();
Session s = getSession();
String sql = "SELECT * FROM student WHERE id_student =:id";
org.hibernate.query.Query query = s.createNativeQuery(sql).addEntity(SuperStudentEntity.class);
query.setParameter("id",id);
SuperStudentEntity student = (SuperStudentEntity) query.getSingleResult();
closeTransactionSesstion();
return student;
}
获得例外
java.lang.ClassCastException: org.codexample.ent.SuperStudentEntity cannot be cast to org.codexample.ent.SuperStudentEntity
at org.codexample.impldao.StudentDAOImpl.getById(StudentDAOImpl.java:45) ~[classes/:na]
at org.codexample.controllers.StudentController.getById(StudentController.java:54) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at ....
浏览器告诉我:
Whitelabel错误页面
此应用程序没有/ error的显式映射,因此您将此视为后备。
Thu May 18 20:02:30 MSK 2017出现意外错误(type = Internal Server Error,status = 500)。 org.codexample.ent.SuperStudentEntity无法强制转换为org.codexample.ent.SuperStudentEntity
我不明白解决问题的方法。