log N
log N

Reputation: 945

Unable to delete an entry-Springs MVC Hibernate

I am trying to delete an entry through spring mvc but I am unable to do it.I am getting 404 error stating requested resource not found.

My controller code is

@RequestMapping("/delete/{user_id}")
    public ModelAndView deleteUser(@PathVariable("user_id")Integer user_id){
        userService.removeUser(user_id);
        return new ModelAndView("redirect:/userList.html");
    }

and that its going to the UserService and from there its going to UserServiceImpl and from there to UserDao to UserDaoImpl whose code is

public void removeUser(Integer user_id){
        User user = (User) sessionfactory.getCurrentSession().load(
                User.class, user_id);
        if (null != user) {
            sessionfactory.getCurrentSession().delete(user);
            System.out.println("Successfully deleted");
        }

I did a hibernate.show_sql=true int the properties file but still for delete I am getting a select statement.

Upvotes: 2

Views: 119

Answers (1)

yname
yname

Reputation: 2245

Wrap delete code with transaction:

Session session = sessionFactory.getCurrentSession();
Transaction t = session.beginTransaction();
User user = (User) session.load(User.class, user_id);
if (null != user) {
        session.delete(user);
        System.out.println("Successfully deleted");
}
session.flush();
t.commit()

Upvotes: 1

Related Questions