我正在尝试使用hibernate框架从mysql数据库中获取记录。为实现这一点,我编写了以下代码。
List addresses = session.createQuery("Select P.address,P.personal_email,P.contact FROM PermanentAddress P,Employee E WHERE E.login_account='"+inf.getlogin_account()+"' AND E.employee_no=P.empno").list();
for (Iterator iterator = addresses.iterator(); iterator.hasNext();){
PermanentAddress pa= (PermanentAddress) iterator.next();
ad.setpaddr(pa.getaddress());
ad.setpemail(pa.getpersonal_email());
ad.setpcontact(pa.getcontact());
System.out.println(ad.getpemail());
}
当我运行此代码时,我收到以下错误
javax.servlet.ServletException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.lms.model.PermanentAddress
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
请告诉我。
答案 0 :(得分:0)
在您的查询中,您将从P.address,P.personal_email,P.contact
而不是整个实体返回字段列表(PermanentAddress
)。尝试这样的事情:
List addresses = session.createQuery("Select P FROM PermanentAddress P,Employee E WHERE E.login_account = '" + inf.getlogin_account() + "' AND E.employee_no=P.empno").list();