当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - How can i identify the generated Object from the Hibernate Query?

问题描述:

I am trying to fetch the records from the mysql database with the hibernate framework. To achieve this i had written the below code.

 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());

}

when i ran this code i am getting the below error

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)

please show me the way.

网友答案:

In your query, you are returning a list of fields (P.address,P.personal_email,P.contact) from PermanentAddress, instead of the whole entities. Try something like this:

 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();
分享给朋友:
您可能感兴趣的文章:
随机阅读: