Reputation:
I have a basic Spring MVC project with the classic dao design for the classes User and Task. I'm using the Hibernate OpenSessionInView Filter pattern for the webapp; but, my UserService
is not working properly, in fact its delete operation. The thing is, i have a simple unit test for this method, and it seems to work fine because when i run it, everything is ok, but when i test via the webapp, like this: curl -X DELETE "http://myhost:port/users/someUserId"
, the delete operation does not work at all, because the object persist once it was invoked.
Here is part of my UserService
:
@Override
public <T> User get(String key, T value) {
User u = new User();
switch (key) {
case "id":
u = (User) session.getCurrentSession().get(User.class, (Serializable) value);
break;
case "email":
u = (User) session.getCurrentSession().createQuery("from User u where u.email = :email").setParameter("email", value.toString()).uniqueResult();
break;
case "username":
u = (User) session.getCurrentSession().createQuery("from User u where u.username = :username").setString("username", (String) value).uniqueResult();
break;
}
if (u != null ) Hibernate.initialize(u.getTasks());
return u;
}
@Override
public void delete(Integer userId) {
session.getCurrentSession().delete(get("id",userId));
}
and UserController
:
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteMemeber(@PathVariable Integer id) {
userService.delete(id);
return "redirect:/";
}
and the unit test for the UserService
delete:
@Test
public void testUserDeleteService() {
List<User> preUsers = userService.findAll();
User userToDelete = userService.findById(3);
userService.delete(3);
List<User>postUsers = userService.findAll();
assertNotEquals(preUsers, postUsers);
assertEquals(preUsers.size() - 1, postUsers.size());
assertFalse(userService.findAll().contains(userToDelete));
assertNull(userService.findById(3));
}
Any ideas for why this is happening?
When the delete method is called, this is the logging:
Hibernate: select user0_.id as id1_1_0_, user0_.email as email2_1_0_, user0_.first_name as first_na3_1_0_, user0_.last_name as last_nam4_1_0_, user0_.password as password5_1_0_, user0_.role as role6_1_0_, user0_.enabled as enabled7_1_0_, user0_.username as username8_1_0_ from users user0_ where user0_.id=?
Hibernate: select user0_.id as id1_1_0_, user0_.email as email2_1_0_, user0_.first_name as first_na3_1_0_, user0_.last_name as last_nam4_1_0_, user0_.password as password5_1_0_, user0_.role as role6_1_0_, user0_.enabled as enabled7_1_0_, user0_.username as username8_1_0_ from users user0_ where user0_.id=?
Hibernate: select tasks0_.user_id as user_id6_1_0_, tasks0_.id as id1_0_0_, tasks0_.id as id1_0_1_, tasks0_.created_on as created_2_0_1_, tasks0_.deadline as deadline3_0_1_, tasks0_.description as descript4_0_1_, tasks0_.name as name5_0_1_, tasks0_.user_id as user_id6_0_1_ from tasks tasks0_ where tasks0_.user_id=?
Hibernate: select tasks0_.user_id as user_id6_1_0_, tasks0_.id as id1_0_0_, tasks0_.id as id1_0_1_, tasks0_.created_on as created_2_0_1_, tasks0_.deadline as deadline3_0_1_, tasks0_.description as descript4_0_1_, tasks0_.name as name5_0_1_, tasks0_.user_id as user_id6_0_1_ from tasks tasks0_ where tasks0_.user_id=?
Upvotes: 3
Views: 7071
Reputation: 620
Try this delete method:
@Override
public void delete(Integer userId) {
session.getCurrentSession().delete(get("id",userId));
session.getCurrentSession().flush();
}
, maybe you just need to flush your active session to see changes.
Upvotes: 1
Reputation: 188
can you change your delete method and try out this?
private boolean deleteById(Class<?> type, Serializable id) {
Object persistentInstance = session.load(type, id);
if (persistentInstance != null) {
session.delete(persistentInstance);
return true;
}
return false;}
simply invoke by deleteById(User.class, id);
is there any child classes or 1tomany or many2many associations?
Upvotes: 0