2011-01-10 58 views
0

更新整個帖子。錯誤:java.lang.ClassCastException

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

我現在返回登錄對象。

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

當沒有匹配的電子郵件時,我得到那裏沒有這樣的用戶,並且此語句也打印出來。 System.out.println("Its Null Value" + newUser);

但是當有電子郵件和密碼匹配。我得到奇怪的錯誤。

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.java: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) at 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

更新

我登錄bean類

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; 

     } 
} 

,並根據raplh查詢也是我更新。

+0

你試圖獲取只有兩個屬性,並在整個OBJ – 2011-01-10 12:02:11

+0

可你剛剛亮我出什麼鑄造編輯 – theJava 2011-01-10 12:04:44

+0

什麼LoginService.authenticate線32是什麼樣子? – 2011-01-10 12:04:52

回答

2

根據你的問題org.hibernate.hql.ast.QuerySyntaxException,我強烈地認爲登錄是一個映射對象。

在這種情況下,你不需要做查詢結果,並用手對象之間的映射,如果你以正確的方式使用Hibernate:

修正(我已經忘了「爲L」查詢的一部分)

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

當你執行像

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

查詢你在讓Hibernate來給你的特定屬性從Login實體emailid。然後Hibernate會以列表的形式給出結果,每個列表項代表[email, id]數組。

如果你只是想查詢符合條件的所有Login實體,那麼這樣做:

FROM Login WHERE email=? AND password=?

然後你的結果會包含Login對象。

但至於你爲什麼試圖投到LoginService,我沒有線索。你混淆了許多不同的Hibernate概念,並試圖將它們混合在一起。

我並不是說聽起來沒有幫助,但我認爲你確實需要正確閱讀Hibernate文檔,並更好地掌握你正在做的事情。

1

嘗試更換此:

return (LoginService) results.get(0); 

與此:

return (Login) results.get(0); 

假設Login是它應該工作一個正確映射休眠實體。

你也應該改變這一行

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

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

而且記住,@拉爾夫的建議是一個非常好的一個關於如何參數化查詢。你應該這樣做。

相關問題