异常在刷新之前保存瞬态实例

时间:2017-09-28 23:13:10

标签: java spring hibernate transient

我有一个User类和一个具有ManyToMany关系的Role类。我制作了Daos和控制器类。我还制作了一个JSP来注册新角色和具有相关角色的新用户。

当我访问用户表单时,我可以使用您各自的角色注册第一个用户,但是当我再次重定向到用户表单并尝试保存另一个用户时,它会抛出一个异常,如:org.hibernate.TransientObjectException: object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.eduardoportfolio.weblibrary.models.Role

我做错了什么?

@Entity
public class User implements UserDetails {
@Id
private String login;
@NotBlank
private String password;
@NotBlank
private String name;
@ManyToMany(fetch=FetchType.EAGER)
private List<Role> roles = new ArrayList<>();


@Entity
public class Role implements GrantedAuthority {
@Id 
private String name;


@Controller
@Transactional
@RequestMapping("/register")
public class RoleController {

@Autowired
RoleDao roleDao;

@RequestMapping("roleForm")
public ModelAndView roleForm(Role role){
    ModelAndView modelAndView = new ModelAndView("user/roleForm");
    modelAndView.addObject("roleList", roleDao.list());
    return modelAndView;
}

@RequestMapping(value="saveRole", method=RequestMethod.POST, name="saveRole")
public ModelAndView saveRole(@Valid Role role, BindingResult bindingResult, RedirectAttributes redirectAttributes){
    if (bindingResult.hasErrors()){
        return roleForm(role);
    }
    roleDao.save(role);

    redirectAttributes.addFlashAttribute("success", "Role successfully 
registered");
    return new ModelAndView("redirect:/register/roleForm");
}

}


@Controller
@Transactional
@RequestMapping("/register")
public class UserController {

@Autowired
UserDao userDao;
@Autowired
RoleDao roleDao;

@RequestMapping("userForm")
public ModelAndView userForm(User user) {
    ModelAndView modelAndView = new ModelAndView("user/userForm");
    modelAndView.addObject("roleList", roleDao.list());
    return modelAndView;
}

@RequestMapping (value="saveUser", method=RequestMethod.POST, name="saveUser")
public ModelAndView saveUser(@Valid User user, BindingResult bindingResult, 
                                                        RedirectAttributes redirectAttributes) {

    if (bindingResult.hasErrors()){
        return userForm(user);
    }
    userDao.save(user);

    redirectAttributes.addFlashAttribute("success", "User successfully registered");
    return new ModelAndView("redirect:/register/userForm");
}

}


@Repository
public class RoleDao {

@PersistenceContext
EntityManager manager;

public void save(Role role){
    manager.persist(role);
}

public void delete(Role role){
    manager.remove(role);
}

public List<Role> list(){
    return manager.createQuery("select p from Role p", 
Role.class).getResultList();
}

}


@Repository
public class UserDao implements UserDetailsService {

@PersistenceContext
private EntityManager manager;

@Override
public UserDetails loadUserByUsername(String userName) throws 
UsernameNotFoundException {

    String jpql = "select u from User u where u.login = :login";
    List<User> users = manager.createQuery(jpql, User.class) .setParameter("login", userName).getResultList();
    if(users.isEmpty()){
        throw new UsernameNotFoundException ("The user "+ userName + "is not found !");
    }

    return users.get(0);
}

public void save(User user){
    user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
    manager.persist(user);
}

public void delete(User user){
    manager.remove(user);
}
}

0 个答案:

没有答案