P
P
P_Alexander2018-06-11 17:00:23
Java
P_Alexander, 2018-06-11 17:00:23

How to fix org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.model.User?

Hello catching this error - org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.model.User; help to understand
the service layer is the @Transactional annotation, at the class level, that is, everything happens in a separate transaction.
There are three roles already recorded in the database, and when I try to update the role, add a link to the user to it, I grab this error. As I understand it, at the time of updating the user object is disabled and he cannot update.
Note - If the role has no users attached yet, then no error occurs.
test method

@Test
    public void writingStartValuesOnDBTest() {
Role role = new Role();
User user3 = new User("Alex3", "Login3",
                "[email protected]", "4444444444", LocalDate.now(), role);
        role = roleService.findByRoleName("user");
        user3.setRole(role);
        role.getUsers().add(user3);
        roleService.update(role);

Update method in service layer
@Override
    public void update(Role role) {
        Role role1 = roleDAO.findById(role.getId_role());
        if(role1 != null){
            role1.setUsers(role.getUsers());  
        }
    }

Any help is appreciated, Thanks!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question