[英]the delete method does't work in Spring Boot&Thymeleaf
i create a simple application in Spring Boot. 我在Spring Boot中创建了一个简单的应用程序。 I have problem with my method delete.
我的方法删除有问题。
I create a UserController like this : @Controller public class UserController { 我这样创建一个UserController:@Controller公共类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 : 在我的UserPrepository中,我有以下方法:
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
User findByEmail(String email);
List<User> findAll();
void deleteUserById(Long id);
}
UserService : UserService:
@Service
public interface UserService extends UserDetailsService {
User findByEmail(String email);
User save(UserRegistrationDto registration);
List<User> findAll();
void deleteUserById(Long id);
}
And UserServiceImpl: 和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; 现在,当我启动我的应用程序并单击删除按钮时,我在网站上遇到此错误:没有EntityManager带有当前线程的实际交易记录-无法可靠地处理“删除”调用; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call
嵌套的异常是javax.persistence.TransactionRequiredException:没有可用于当前线程进行实际事务处理的EntityManager-无法可靠地处理“删除”调用
What doesn't mine ? 什么不是我的? How to fix it ?
如何解决?
Only CRUD methods (CrudRepository methods) are by default marked as transactional. 默认情况下,只有CRUD方法(CrudRepository方法)被标记为事务性的。 If you are using custom query methods you should explicitly mark it with
@Transactional
annotation. 如果使用自定义查询方法,则应使用
@Transactional
注释明确标记它。
so Put @Transactional on deleteUserById method in repository classs like below. 因此,将@Transactional放在以下存储库类中的deleteUserById方法上。
@Transactional
void deleteUserById(Long id);
或者,您也可以使用deleteUserById
的默认方法替换deleteUserById
void deleteById(ID id)
删除具有给定ID的实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.