2

这是代码:

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;
    }

错误:</p>

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

4 回答 4

3

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

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

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

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];
于 2013-09-03T06:13:31.430 回答
1

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

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

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

于 2013-09-03T05:50:13.227 回答
1

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

String hql="select new yourpath.Users(user.name,user.password) from Users user
where user.name=:name";
于 2013-09-03T05:22:08.603 回答
-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.
 }
于 2013-09-03T05:34:30.250 回答