2

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)

2
  • it is good to select hibernet tag also Commented Sep 3, 2013 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. Commented Sep 3, 2013 at 5:23

4 Answers 4

3

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

-1

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

1 Comment

No need to do it, hql should be just fixed or return different data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.