V
V
Virgil Merkel2020-08-03 17:52:56
PostgreSQL
Virgil Merkel, 2020-08-03 17:52:56

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

and such a controller: (tried to do deleteByAdminproductname() and find by id, delete and save)
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";
    }
}


Gives an error like this
5f2824b871dfb688017422.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Orkhan, 2020-08-03
Hasanly @azerphoenix

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

Most likely this is superfluous
adminProductRepo.save(admrem);
4) There may also be a problem here. For example, in camelCase.
deleteByAdminproductname()
Because you have an entity called AdminProduct and a name field
. And you have a request structure:
Adminproductname
Something is also wrong ...
Well, in the end, set breakpoints, turn on debugging and see what error occurs on the server. Maybe your request doesn't even reach the controller due to a bug in Spring Security

V
Virgil Merkel, 2020-08-03
@blrmyfc

5f2875e1b5a04911235911.jpeg
It does not work from this site))

D
Dmitry Roo, 2020-08-04
@xez

The logs you provided says:

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

This means that it's time to deal with transactions.
Read, for example:
https :
//habr.com/ru/company/otus/blog/431508/akorsa.ru/2016/08/kak-na-samom-dele-rabotaet-trans...
You can see examples from Eugen Paraschiv :
https://www.baeldung.com/transaction-configuration...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question