java.lang.ClassCastException:[Ljava.lang.Object;无法转换为com.xl.entity.Users

时间:2013-09-03 05:19:10

标签: java hibernate hql

以下是代码:

public Users login(String username) {
        Users user=null;
        try {
            String hql="select user.name,user.password from Users user where user.name=:name";
            Query query = session.createQuery(hql);
            query.setString("name", username);
            user=(Users) query.list().get(0);
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return user;
    }

错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xl.entity.Users
    at com.xl.impl.HouseDaoImpl.login(HouseDaoImpl.java:51)
    at com.xl.biz.HouseBiz.login(HouseBiz.java:25)
    at com.xl.Servlet.UserServlet.doGet(UserServlet.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

(翻译由谷歌翻译提供)

4 个答案:

答案 0 :(得分:3)

如果您需要Users对象,请将您的查询编辑为

String hql="from Users user where user.name=:name";

否则结果将是一个对象数组,因此您需要使用变换器或简单地将其强制转换为对象[]:

String hql="select user.password from Users user where user.name=:name";
Query query = session.createQuery(hql);
query.setString("name", username);
Object[] result =(Object[]) query.list().get(0);
String password = result[0];

答案 1 :(得分:1)

您可以为Users创建另一个构建函数public Users(String name, String password)并将查询更改为

String hql="select new yourpath.Users(user.name,user.password) from Users user
where user.name=:name";

答案 2 :(得分:1)

您需要以这种方式支持根实体ResultTransformer

Query query = session.createQuery(hql);
query.setResultTransformer(RootEntityResultTransformer.INSTANCE);
query.list()

使用投影(选择列表)会产生Object[]类型的结果:ResultTransformer将创建User对象,并将原始Object[]映射到正确的User 1}}的属性

答案 3 :(得分:-1)

由于query.list()会返回List,因此您在此处投射列表内容以键入User,现在,异常显示为Object can not be cast to User。请确保,您是否以用户的形式获取内容。

您可以进行检查:

 if(query.list() instanceOf User){
        // can cast to user.
 }else{
        // can not cast to User.
 }