Hello,
Im developing a web application and I use MySql Server 5.1 and Hibernate.
Today I changed my tables - before, I didnt use ids to identify the user, now I do.
But now, Ive got a lot of problems. For example, when I look for a username in order to see if this user is already registered, I get a java.lang.IndexOutOfBoundsException. I understand the message: when there is no user with this username, nothing can be returned (might this be the cause?). But how shall I check then if the user does exist or not? I can not say: if I get a errormessage, the user does not exists - that does not sound like a good solution!
I will show you now my old and new configurations, maybe you can help me!
The Tables:
Code:
// NEW
create table users (
id int NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
password varchar(50) NOT NULL,
enabled boolean NOT NULL,
primary key(id),
unique(username))
ENGINE=InnoDB;
// OLD
create table users (
username varchar(50) NOT NULL,
password varchar(50) NOT NULL,
enabled boolean NOT NULL,
primary key(username))
ENGINE=InnoDB;
The HibernateDao (Snippet)
Code:
// NEW
public Users loadUsersByUsername(String username) {
List<Users> list = this.sessionFactory.getCurrentSession().createQuery("from Users users where users.username=?").setParameter(0, username).list();
Users u = list.get(0);
return u;
}
// OLD
public Users loadUsersByUsername(String username) {
return (Users) this.sessionFactory.getCurrentSession().get(Users.class, username);
}
calling the HibernateDao-Method
Code:
// NEW
try {
Users u = projectDao.loadUsersByUsername(username);
return false;
} catch (Exception e) {
logger.warn("loading the users object did not work, " + e);
return true;
}
// OLD
Users u = projectDao.loadUsersByUsername(username);
if (u == null) {
logger.info("checkUsername(..): username not in use");
return true;
} else {
logger.info("checkUsername(..): username already in use");
return false;
}
I would be very grateful, if you could help me, this is very important to me! thank you! :-)
EDIT: The errormessage :-) The user does not exist, so I do no expect to get an object, but I need to check somehow, if this user exist. how can I do this?
Code:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0