Hi,
I am having some problem with the Hibernate + Spring testing framework. I have written a test that saves a record in a transaction. When I use the get method of HibernateTemplate, it works fine but when I try to execute the find method. It does not return the object that I saved in the same transaction. Below is my method that saves the user record in a transaction.
Code:
User newUser = new User();
user.setLoginName("kashif.nazar");
dao.saveUser(newUser);
userId = newUser.getUserId();
and below are the methods that I am using to retrieve the saved object.
Code:
// this test is working fine
public void testGetUser() throws Exception {
User user = dao.getUser(userId);
assertNotNull(user);
assertEquals("kashif.nazar", user.getLoginName());
}
// this test is NOT working fine
public void testGetUserByLoginName() throws Exception {
User user = dao.getUserByLoginName("kashif.nazar");
assertNotNull(user);
}
As you can see in the above comment, the second method does not pass the test because the user object returned is null. Here is the definition of the method, getUserByLoginName.
Code:
public User getUserByLoginName(String loginName) {
@SuppressWarnings("unchecked")
List<User> users = getHibernateTemplate().
find("from User u where u.loginName = ?",
new Object[]{loginName});
if (users != null && users.size() > 0) {
return users.get(0);
}
return null;
}
Please help me.
Regards,
Kashif