javaspring-bootjpahttp-delete

Delete an Object from the H2 database by ID without autogenerated ( The given id must not be null!)


I made a list of rooms without an autogenerated ID because the ID is also the room number and I want to give them this number manually. But in postman, I can not delete one room from the list/database(h2). I get the Error:

ERROR 12848 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!] with root cause

I didn't find anything for delete yet, only post requests, which work fine for me. Class:

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "roomNr")
public class Room {
    @Id
    private Integer roomNr;

Controller:

 @CrossOrigin
 @DeleteMapping("{roomNr}")
 public void deleteOneZimmer(Integer roomNr){
        roomService.deleteOneRoom(roomNr);

Service:

public void deleteOneRoom(Integer roomNr){
        roomRep.deleteById(roomNr);
    }

and in postman, I get status: 500, error: Internal Server Error.

the path in postman is: room/1 and any other room with another ID is not working either.

This is my first post, so sorry if I am missing some info to solve that problem.


Solution

  • I got the problem: I simply forgot @Pathvaribale in the Controller Class.