Answer the question
In order to leave comments, you need to log in
Why is it throwing an error when deleting a row in Spring Boot using JPA?
I have a table admin_productions with columns [id, adminproductname, adminproducttotal] I
have a Crud repository with this code:
package com.example.beton.repos;
import com.example.beton.domain.AdminProductions;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface AdminProductRepo extends CrudRepository<AdminProductions, Integer> {
List<AdminProductions> findByAdminproductname(String adminproductname);
List<AdminProductions> deleteByAdminproductname(String adminproductname);
}
package com.example.beton.controller;
import com.example.beton.domain.AdminProductions;
import com.example.beton.repos.AdminProductRepo;
import com.example.beton.repos.WarehouseRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@Controller
@PreAuthorize("hasAuthority('ADMIN')")
public class RemoveController {
@Autowired
private AdminProductRepo adminProductRepo;
@Autowired
private WarehouseRepo warehouseRepo;
@GetMapping("/removeproduct")
public String remPage(Map<String, Object> model){
Iterable<AdminProductions> adminproductions = adminProductRepo.findAll();
model.put("adminproductions", adminproductions);
return "removeproduct";
}
@PostMapping("/removeproduct")
public String removeAdminProduct(@RequestParam String removname, Map<String, Object> model){
List<AdminProductions> rmadm = adminProductRepo.findByAdminproductname(removname);
for (AdminProductions admrem : rmadm){
adminProductRepo.deleteById(admrem.getId());
adminProductRepo.save(admrem);
}
Iterable<AdminProductions> adminproductions = adminProductRepo.findAll();
model.put("adminproductions", adminproductions);
return "removeproduct";
}
}
Answer the question
In order to leave comments, you need to log in
Hello!
The fact that you see a 403 error (Forbidden) doesn't really mean anything.
1) If you have Spring Security enabled, then check if the user can make the request you need. @PreAuthorize("hasAuthority('ADMIN')")
The user must be an admin
2) Check if you are processing post || get request.
3) There is also an error in this section of the code. You first delete admrem and then try to save the deleted admrem.
for (AdminProductions admrem : rmadm){
adminProductRepo.deleteById(admrem.getId());
adminProductRepo.save(admrem);
}
adminProductRepo.save(admrem);
deleteByAdminproductname()
Adminproductname
The logs you provided says:
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question