Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is the code:

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

Error:

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)

(Translations courtesy Google Translate)

share|improve this question
 
it is good to select hibernet tag also –  Prabhaker Sep 3 '13 at 5:20
 
What is the question? You might want to consider looking at the help page for some examples on what to include in your questions. Welcome to SO. –  Qben Sep 3 '13 at 5:23
add comment

4 Answers

You can create another constructor for Users as public Users(String name, String password) and change query to

String hql="select new yourpath.Users(user.name,user.password) from Users user
where user.name=:name";
share|improve this answer
add comment

You need a ResultTransformer backed on root entity in this manner

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

Using a projection (the select list) will produce a result of Object[] type: the ResultTransformer will create a User object and maps raw Object[] to correct User's property

share|improve this answer
add comment

If you need the Users object, edit your query to be like

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

Otherwise the result will be an array of objects so you need to either use a transformer or simply cast it to 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];
share|improve this answer
add comment

Since query.list() returns List, and here you are casting the content of the list to type User, Now, as exception says Object can not be cast to User. Please make sure, are you getting the content in form of user.

You can make a check:

 if(query.list() instanceOf User){
        // can cast to user.
 }else{
        // can not cast to User.
 }
share|improve this answer
 
No need to do it, hql should be just fixed or return different data type. –  Alex Sep 3 '13 at 5:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.