简体   繁体   中英

Error: java.lang.ClassCastException

Updating the entire post.

public Login authenticate(Login login) {
        String query = "SELECT 1 FROM Login AS l WHERE l.email=? AND l.password=?";
        Object[] parameters = { login.getEmail(), login.getPassword() };
        List resultsList = getHibernateTemplate().find(query,parameters);
        if ( resultsList.size() == 1 ) {
        results = (Login)resultsList.get(0);
        System.out.println(results);
        } else {
            System.out.println("Error dude.... ");
        // error no entity or mutiple entities
        }
        return results;
}

I now return Login Objects.

private void checkLogin() {
        form.commit();

        Login newUser = new Login();
        newUser = ilogin.authenticate(loginbean);
        System.out.println("Its Null Value" + newUser);
        if (newUser == null) {
            getWindow().showNotification("Login failed", LOGIN_ERROR_MSG,
                    Notification.TYPE_WARNING_MESSAGE);
        } else {
            System.out.println(newUser);
            getApplication().setUser(newUser);
        }
    }

When there is no matching email, i get there is no such user and also this statement does get printed out. System.out.println("Its Null Value" + newUser);

But when there is a email and password matching. I get weird error.

Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.intermedix.domain.Login at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507) at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161) at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1154) at com.vaadin.ui.Button.fireClick(Button.java:371) at com.vaadin.ui.Button.changeVariables(Button.java:193) at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1094) at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:590) at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:266) at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:476) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.j ava:511) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) at org.mortbay. jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.intermedix.domain.Login at com.intermedix.services.LoginService.authenticate(LoginService.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) a t org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy32.authenticate(Unknown Source) at com.intermedix.ui.LoginDailog.checkLogin(LoginDailog.java:106) at com.intermedix.ui.LoginDailog.access$0(LoginDailog.java:102) at com.intermedix.ui.LoginDailog$1.buttonClick(LoginDailog.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:487) ... 26 more

Updating

My Login bean class

package com.intermedix.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class Login {
      public Login(){}
        private Long id = null;
        private String email;
        private String password;

        public Login(String email, String password)
        {
            this.email = email;
            this.password = password;
        }

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;

        }
}

and also i updated according to raplh the query.

When you execute a query like

SELECT email, id FROM Login WHERE email=? AND password=?

You're asking Hibernate to give you the specific properties email and id from the Login entity. Hibernate will then give you the results as a list of arrays, with each list item representing the [email, id] array.

If you just want to query for all Login entities that match your criteria, then do this:

FROM Login WHERE email=? AND password=?

And then your result list will contain Login objects.

But as to why you're trying to cast to LoginService , I haven't a clue. You're confusing a number of different Hibernate concepts and trying to mash them all together.

I don't mean to sound unhelpful, but I think you really need to go and read the Hibernate docs properly, and get a better grasp of what you're doing.

According to your question org.hibernate.hql.ast.QuerySyntaxException , I strongly assume that Login is a mapped object.

In this case you do not need to do the mapping between the query result and the object by hand, if you use Hibernate in the correct way:

corrected (i have forgot the "AS l" part of the query)

   List results = createQuery(
     "SELECT l FROM login AS l WHERE l.email=:email AND l.password=:password")
     .setParameter("email",login.getEmail())
     .setParameter("password",login.getPassword()).list();
   if (results.isEmpty()) {
     //.. login failed
   } else if (result.size() > 1) {
     throw new SomethingWrongException();
   } else {
     Login login = (Login) results.get(0);
   }

try replacing this:

 return (LoginService) results.get(0);

with this:

 return (Login) results.get(0);

Assuming that Login is a properly mapped hibernate entity it should work.

you should also change this line

String query = "SELECT 1 FROM Login AS l WHERE l.email=? AND l.password=?";

to

String query = "SELECT login FROM Login AS login WHERE login.email=? AND login.password=?";

Also keep in mind that @Ralph's suggestion is a very good one for how to parametrize queries. You should do it like that.

With your updated question:

Update your query:

String query = "SELECT l FROM Login AS l WHERE l.email=? AND l.password=?";

And please read the manual: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM