我得到一個ClassCastException當迭代列表從HQL類轉換異常在迭代列表中返回從HQL
在該行
UserPojo u=(UserPojo)userList.get(0);
UserDao.java
public class UserDao
{
Session session = null;
Transaction tx=null;
public int validateUser(UserManagedBean user)
{
int validateFlag=-1;
try
{
tx = session.beginTransaction();
Query q = session.createQuery("select user1.userId, user1.userName, user1.password from UserPojo user1 where user1.userName=:userName and user1.password=:password");
//Query q=session.getNamedQuery("validateUser");
//Query q=session.getNamedQuery("proc_validateUser");
q.setParameter("userName", user.getUserName());
q.setParameter("password", user.getPassword());
List<UserPojo> userList = q.list();
//List<UserPojo> userList = (List<UserPojo>)q.list();
int rowCount = userList.size();
if (rowCount == 1)
{
UserPojo u=(UserPojo)userList.get(0);
System.out.println("U.userId:"+u.getUserId());
//user.setUserId(userList.get(0).getUserId());
validateFlag = 1;
}
else if (rowCount == 0)
{
validateFlag = 0;
}
else if (rowCount > 1)
{
validateFlag = -1;
}
tx.commit();
}
catch (HibernateException e)
{
LOG.logp(Level.SEVERE, "UserDao", "validateUser", "Caught HibernateException During User Validity Check", e);
try
{
tx.rollback();
}
catch(HibernateException ex)
{
LOG.logp(Level.SEVERE, "UserDao", "validateUser", "Caught HibernateException While Rolling Back Transaction", ex);
}
}
return validateFlag;
}
public UserDao()
{
session = HibernateUtil.getSessionFactory().getCurrentSession();
}
private static final Logger LOG = Logger.getLogger(UserDao.class.getName());
}
retured
我設法做如下修改代碼'名單
我編輯了我的答案。檢查出來 –
是的,這是我想出的第一個解決方案,但是使用MappedPojo'會從所有列中加載數據 (如果我錯了,請糾正我),如果表格包含少量列,那很好。當我們只想獲取少數列(例如從大表)而不是使用'from MappedPojo'時,您可以將sql與選定的列放入過程中,並調用過程將返回'List',並且它將設置required(not所有,而不是來自MappedPojo')MappedPojo.Pls的屬性正確,並建議我從列數過多的表中提取少量列的替代方法。 –
user3157115