0

i create a simple application in Spring Boot. I have problem with my method delete.

I create a UserController like this : @Controller public class UserController {

@Autowired
UserRepository userRepository;

@RequestMapping(value = "/students", method = RequestMethod.GET)
public String tables(Model model){
    model.addAttribute("users", userRepository.findAll());
    return "students";
}


@RequestMapping(value = "/delete/{id}", method = GET)
public String delete(Model model, @PathVariable("id") Long id){
    User user = userRepository.findById(id)
        .orElseThrow(() -> new IllegalArgumentException("Niepoprawne id : " + id));
    userRepository.deleteUserById(id);
    model.addAttribute("students", userRepository.findAll());
    return "redirect:/students";
}}

In my UserPrepository i have this method :

@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    User findByEmail(String email);
    List<User> findAll();
   void deleteUserById(Long id);

}

UserService :

@Service
public interface UserService extends UserDetailsService {

    User findByEmail(String email);
    User save(UserRegistrationDto registration);
    List<User> findAll();
    void deleteUserById(Long id);

}

And UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;


    public User findByEmail(String email){
        return userRepository.findByEmail(email);
    }


    private Collection<? extends GrantedAuthority> mapRolesToAuthorities(Collection<Role> roles){
        return roles.stream()
            .map(role -> new SimpleGrantedAuthority(role.getName()))
            .collect(Collectors.toList());
    }

    public List<User> findAll(){
       return userRepository.findAll();
    }


    public void deleteUserById(Long id) {
        userRepository.deleteUserById(id);
    }
}

In frontend i create a simple table like this:

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0" >
                            <thead>
                            <tr>
                                <th>IMIE</th>
                                <th>NAZWISKO</th>
                                <th>EMAIL</th>
                                <th>AKCJA</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                            <tr th:each="user :${users}">
                                <td th:text="${user.firstName}"></td>
                                <td th:text="${user.lastName}"></td>
                                <td th:text="${user.email}"></td>
                                <td>
                                    <!--<a th:href="${'/students/delete/' + user.id}" class="btn btn-danger">UsuĊ„</a>-->
                                <td><a th:href="@{/delete/{id}(id=${user.id})}">Delete</a></td>
                                </td>
                            </tr>
                            </tr>
                            </tbody>
                        </table>

Now, when i start my application and click delete button i have this error on the website : No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

What doesn't mine ? How to fix it ?

mattt
  • 195
  • 2
  • 5
  • 14

2 Answers2

2

Only CRUD methods (CrudRepository methods) are by default marked as transactional. If you are using custom query methods you should explicitly mark it with @Transactional annotation.

so Put @Transactional on deleteUserById method in repository classs like below.

@Transactional
void deleteUserById(Long id);

Refer this

Alien
  • 15,141
  • 6
  • 37
  • 57
2

Or you can replace deleteUserById by the default method of CrudRepository void deleteById(ID id) whitch deletes the entity with the given id.

Abder KRIMA
  • 3,418
  • 5
  • 31
  • 54