Im trying to collect the result from the query but the list is empty when I check the log in my console. The login action works perfectly but obtain the query result is somewhat not happening. I also used size() to check but it also brings a zero size. What am I missing here
See my dao class:
UserDAO.java
public User userLogin(String email, String password) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
User userinfo = new User();
try {
String hql = "select * from user where email =:email and password =:password";
SQLQuery query = session.createSQLQuery(hql);
query.addEntity(User.class);
query.setParameter("email", email);
query.setParameter("password", password);
List rows = query.list();
System.out.println("Total Number Of Records : " + rows.size());
Iterator myItr = rows.iterator();
while (myItr.hasNext()) {
Object ui = (Object) myItr.next();
userinfo = (User) ui;
System.out.println("Username : " + userinfo.getUsername());
}
if (userinfo != null) {
System.out.println("User Retrieved from DB::" + userinfo);
}
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
// close your session
session.close();
}
return userinfo;
}
Try using a Query
hibernate and use uniqueResult()
:
String hql = "from user where email =:email and password =:password";
Query query = session.createQuery(hql);
query.setParameter("email", email);
query.setParameter("password", password);
User userinfo = (User) query.uniqueResult();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.