我有一个基本的Spring MVC项目,其中包含用户和任务类的经典dao设计。我正在使用Web应用程序的Hibernate OpenSessionInView过滤器模式;但是,我的UserService
无法正常工作,实际上是它的删除操作。事情是,我有一个简单的单元测试这个方法,它似乎工作正常,因为当我运行它,一切都很好,但当我通过webapp测试,像这样:curl -X DELETE "http://myhost:port/users/someUserId"
,删除操作根本不起作用,因为一旦调用它就会持久化。
以下是我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));
}
和UserController
:
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteMemeber(@PathVariable Integer id) {
userService.delete(id);
return "redirect:/";
}
和UserService
删除的单元测试:
@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));
}
有关为何发生这种情况的任何想法?
调用delete方法时,这是日志记录:
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=?
答案 0 :(得分:0)
你可以改变你的删除方法并尝试一下吗?
private boolean deleteById(Class<?> type, Serializable id) {
Object persistentInstance = session.load(type, id);
if (persistentInstance != null) {
session.delete(persistentInstance);
return true;
}
return false;}
只需调用 deleteById(User.class,id);
是否有任何儿童班或1tomany或many2many协会?
答案 1 :(得分:0)
尝试此删除方法:
@Override
public void delete(Integer userId) {
session.getCurrentSession().delete(get("id",userId));
session.getCurrentSession().flush();
}
,也许您只需要刷新活动会话即可查看更改。