日期:2014-05-20  浏览次数:21117 次

类的强制转换
以下代码执行的时候发生错误,提示为:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.ClassManer.Hibernate.User

应该是类型转换不了,不知道怎么解决

public class LoginAction extends ActionSupport implements ServletRequestAware {

private String username;
private String password;
private HttpServletRequest request;

@Override
public String execute() throws Exception {
// SessionFactory sf=new AnnotationConfiguration().
// configure().buildSessionFactory();
try {
Configuration conf= new Configuration().configure();
SessionFactory sf =conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();

username = this.getUsername();
password = this.getPassword();
String sqlQuery = "select u.* from user u where " + "u.username= '"
+ username + "'";
//session.createQuery(arg0)
Query lQuery = session.createSQLQuery(sqlQuery);
ArrayList<?> userlist = (ArrayList<?>) lQuery.list();
tx.commit();
session.close();

User user = new User();
if ((null != userlist) && (userlist.size() > 0)) {
user = (User) (userlist.get(0));
if (!user.getPassword().equals(password)) {
return "failer";
}
} else
return "failer";
} catch (HibernateException e) {
e.printStackTrace();
return "failer";
}
return "success";
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub


}

/**
* @param password
* the password to set
*/
public void setPassword(String password) {
this.password = password;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param request
* the request to set
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}

/**
* @return the request
*/
public HttpServletRequest getRequest() {
return request;
}

/**
* @param username
* the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

}


------解决方案--------------------
探讨

user是表名,就是小写的
sql语句去掉select之后出错了

------解决方案--------------------
String sqlQuery = "select u.* from user u where " + "u.username= '"
+ username + "'";

这里的user对应的是你的实体Bean名称