我想插入列表項對象,這是代碼:我不能插入列表項對象
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);
終於完成了,非常感謝你! :D – peasantScript