删除方法在 Spring Boot&Thymeleaf 中不起作用

the delete method does't work in Spring Boot&Thymeleaf

我在 Spring Boot 中创建了一个简单的应用程序。我的删除方法有问题。

我这样创建了一个 UserController : @控制器 public class 用户控制器 {

@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";
}}

在我的 UserPrepository 中我有这个方法:

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

}

用户服务:

@Service
public interface UserService extends UserDetailsService {

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

}

和 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);
    }
}

在前端,我创建了一个简单的 table,如下所示:

<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>

现在,当我启动我的应用程序并单击删除按钮时,网站上出现此错误:当前线程没有可用的实际事务的 EntityManager - 无法可靠地处理 'remove' 调用;嵌套异常是 javax.persistence.TransactionRequiredException:当前线程没有可用的实际事务的 EntityManager - 无法可靠地处理 'remove' 调用

什么不是我的?如何解决?

只有 CRUD 方法(CrudRepository 方法)默认标记为事务性的。如果您使用自定义查询方法,您应该使用 @Transactional 注释明确标记它。

所以将 @Transactional 放在存储库类中的 deleteUserById 方法上,如下所示。

@Transactional
void deleteUserById(Long id);

参考

或者您可以将 deleteUserById 替换为 CrudRepository void deleteById(ID id) 的默认方法,后者会删除具有给定 ID 的实体。