2015-11-15 102 views
2

我想插入列表項對象,這是代碼:我不能插入列表項對象

public User login(String username, String password) { 
    String stringQuery = "SELECT * FROM User U WHERE U.username = :username AND U.password = :password"; 
    Query query = HibernateUtil.getCurrentSession().createSQLQuery(stringQuery); 
    query.setParameter("username", username); 
    query.setParameter("password", password); 

    User user; 

    List<User> userList = query.list(); 

    if (userList.size() == 0){ 
     return null; 
    } 

    user = (User) userList.get(0); 

    return user; 
} 

這裏User.class

@Entity 
public class User { 
    private int id; 
    private String username; 
    private String password; 

    @Id 
    @Column(name = "id") 
    public int getId() { 
     return id; 
    } 

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

    @Basic 
    @Column(name = "username") 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Basic 
    @Column(name = "password") 
    public String getPassword() { 
     return password; 
    } 

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

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     User user = (User) o; 

     if (id != user.id) return false; 
     if (password != null ? !password.equals(user.password) : user.password != null) return false; 
     if (username != null ? !username.equals(user.username) : user.username != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = id; 
     result = 31 * result + (username != null ? username.hashCode() : 0); 
     result = 31 * result + (password != null ? password.hashCode() : 0); 
     return result; 
    } 
} 

在此行中我得到錯誤:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.project.server.base.User

user = (User) userList.get(0); 

回答

0

當你確信這個查詢將返回唯一結果使用uniqueResult()

Convenience method to return a single instance that matches the query, or null if the query returns no results.

public User login(String username, String password) { 
    String stringQuery = "FROM User U WHERE U.username = :username AND U.password = :password"; 
    Query query = HibernateUtil.getCurrentSession().createQuery(stringQuery); 
    query.setParameter("username", username); 
    query.setParameter("password", password); 
    return (User)query.uniqueResult(); 
} 

或者,如果你想要做你的方式: -

public User login(String username, String password) { 
    String stringQuery = "SELECT * FROM User U WHERE U.username = :username AND U.password = :password"; 
    Query query = HibernateUtil.getCurrentSession().createQuery(stringQuery); 
    query.setParameter("username", username); 
    query.setParameter("password", password); 
    List<User> userList = (ArrayList<User>)query.list(); 
    if (userList.size() == 0){ 
     return null; 
    } 
    return userList.get(0); 
} 
+0

你的第二個代碼與他的代碼有什麼不同? (不包括'query.list()'可能不會返回一個'ArrayList'的事實,並且你的代碼會比他更早失敗) – user3707125

+0

你的代碼和我的一樣是正確的,但是,我不知道爲什麼我仍然有同樣的錯誤! – peasantScript

+0

@peasantScript,這段代碼不正確,Hibernate無法使用一些魔法確定結果類型(所以它會返回一個對象數組 - 所有接收到的列),您是否嘗試過我描述的解決方案? – user3707125