JavaSheriff
JavaSheriff

Reputation: 7665

org.springframework.dao.EmptyResultDataAccessException: No class X entity with id Y exists

Need some help understanding this exception

org.springframework.dao.EmptyResultDataAccessException: No class X entity with id Y exists!

this is the exception:

11/24/2020 07:07:49,677 [ERROR] [http-nio-8081-exec-10] org.springframework.boot.web.servlet.support.ErrorPageFilter.forwardToErrorPage(183) – Forwarding to error page from request [/api/printer/XRX0001193] due to exception [No class com.xerox.model.Printers entity with id XRX0001193 exists!]
org.springframework.dao.EmptyResultDataAccessException: No class com.xerox.model.Printers entity with id XRX0001193 exists!
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.lambda$deleteById$0(SimpleJpaRepository.java:176) ~[spring-data-jpa-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository$$Lambda$1424/1512319807.get(Unknown Source) ~[?:?]
    at java.util.Optional.orElseThrow(Optional.java:290) ~[?:1.8.0_05]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.deleteById(SimpleJpaRepository.java:175) ~[spring-data-jpa-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]

I an calling the service from angular using HttpClient and delete:

 clickRemove(id:string){
    if(confirm("Are you sure to delete printer with Id: "+id)) {
        this.httpClient.delete(this.base +"/api/printer/"+ id).subscribe(data => {
          console.log(data);
        });
      }

this is the model:

@Entity
@Table(name = "XEROX_PRINTERS")
public class Printers implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "D_UNIQUE_ID")
    private String dUniqueId;

And the controller

Controller:
@RequestMapping("/api/printer")
@RestController
public class PrintersController {

    @DeleteMapping("/{id}")
    public void remove(@PathVariable("id") String id){
        printersService.remove(id);
      } 

Upvotes: 0

Views: 3859

Answers (1)

Boommeister
Boommeister

Reputation: 2107

The EmptyResultDataAccessExceptionis pretty self explanatory, when looking at the spring docs you can find:

Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned.

When attempting to delete an entity which has already been deleted, or simply does not exist, an EmptyResultDataAccessException is thrown. Handle this exception gracefully to ensure the REST service client receives a consistent response.

Your code looks correct, so to not get the Exception simply save printer with the id before you delete it?

Upvotes: 2

Related Questions