I'm using v2.0.3 and attempting to write some unit tests for my Hibernate DAO implementation.
Part of the unit tests is cleaning up created objects in the database, but the objects don't get deleted unless I delete the object twice.
My test looks like this:
Code:
// ...
HibernateUser createdUser =
(HibernateUser)userMgr.createUser(FOO_EMAIL, FOO_PASSWORD);
usersToDelete.add(createdUser);
session.flush();
// ...
HibernateUser retrievedUser =
(HibernateUser)userMgr.getUser(FOO_EMAIL, FOO_PASSWORD);
// ...
the create user method looks like:
Code:
public User createUser(String email, String password)
throws UserException
{
HibernateUser user = new HibernateUser(email, password);
Session session = getSession();
Serializable id = null;
try
{
id = session.save(user);
session.flush();
}
catch (HibernateException err)
{
throw new UserException(email, err);
}
user.setId(id);
return user;
}
The getUser method looks like:
Code:
public User getUser(String email, String password)
throws UserException
{
Session session = getSession();
Query query = null;
List results = null;
try
{
query = session.getNamedQuery(
(QRY_USER_BY_EMAIL_PASSWORD);
query.setParameter(PARAM_EMAIL, email);
query.setParameter(PARAM_PASSWORD, password);
results = query.list();
}
catch (Exception err)
{
throw new UserException(email, err);
}
if (!results.isEmpty())
{
user = (User)results.get(0);
}
return user;
}
My clean up code is:
Code:
private void deleteUsers() throws Exception
{
Iterator users = usersToDelete.iterator();
Object obj = null;
while (users.hasNext())
{
obj = users.next();
session.delete(obj);
}
session.delete("from HibernateUser");
}
If I don't put that last delete, the created user doesn't get deleted. Even more odd, that last delete causes the created user to get deleted but not other records in the table that I inserted manually.
Is this the same problem, and is there a better solution than dual deletes?