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";
}
}
@PreAuthorize("hasAuthority('ADMIN')")
Пользователь должен быть админомfor (AdminProductions admrem : rmadm){
adminProductRepo.deleteById(admrem.getId());
adminProductRepo.save(admrem);
}
adminProductRepo.save(admrem);
deleteByAdminproductname()
Adminproductname
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call