title: Hi,In springMvc Rest Onetoone CRUD of client side. when i am inserting data it was working just fine and shows error while updating and deleteing
body: <p>==&gt;here when i want to insert data it just working fine,The Only problem is when am updating or deleting it was showing error.So, how to make it work for updating and deleting.</p>
  <p><a href="https://i.stack.imgur.com/alhIB.png" rel="nofollow noreferrer">CRUD IMAGE</a><br>
  <a href="https://i.stack.imgur.com/54rRB.png" rel="nofollow noreferrer">Error image when deleting</a><br>
  <a href="https://i.stack.imgur.com/8kF6H.png" rel="nofollow noreferrer">Data base image rever engineering</a></p>
  <pre><code>application.properties
  /***************************/
  crm.rest.url=http://localhost:8080/restonetoone/api/details 
  </code></pre>
  <blockquote>
  <p>this Details.java is OnetoOne with Student.java (these are DTO's)</p>
  </blockquote>
  <pre><code> Details.java
      /*******************/
      package com.rest.entity;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.OneToOne;
      
      @Entity
      public class Details {
      
          @Id
          private int id;
          
          private String city;
          
          private long mobileno;
          
          @OneToOne
          private Student student;
          
          public Details(){
              
          }
      
      
          public Details(int id, String city, long mobileno, Student student) {
              super();
              this.id = id;
              this.city = city;
              this.mobileno = mobileno;
              this.student = student;
          }
      
      
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getCity() {
              return city;
          }
      
          public void setCity(String city) {
              this.city = city;
          }
      
          public long getMobileno() {
              return mobileno;
          }
      
          public void setMobileno(long mobileno) {
              this.mobileno = mobileno;
          }
      
          public Student getStudent() {
              return student;
          }
      
          public void setStudent(Student student) {
              this.student = student;
          }
      
          @Override
          public String toString() {
              return &quot;Details [id=&quot; + id + &quot;, city=&quot; + city + &quot;, mobileno=&quot; + mobileno + &quot;, student=&quot; + student + &quot;]&quot;;
          }   
      }
  </code></pre>
  <blockquote>
  <p>the Student.java class</p>
  </blockquote>
  <pre><code> Student.java
      /*****************/
      package com.rest.entity;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      
      @Entity
      public class Student {
      
          @Id
          private int id;
          
          private String name;
          
          private int clas;
          
          public Student() {
              
          }
      
      
          public Student(int id, String name, int clas) {
              super();
              this.id = id;
              this.name = name;
              this.clas = clas;
          }
      
      
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public int getClas() {
              return clas;
          }
      
          public void setClas(int clas) {
              this.clas = clas;
          }
      
          @Override
          public String toString() {
              return &quot;Student [id=&quot; + id + &quot;, name=&quot; + name + &quot;, clas=&quot; + clas + &quot;]&quot;;
          }
          
          
      }
  </code></pre>
  <blockquote>
  <p>the DetailsService layer</p>
  </blockquote>
  <pre><code> DetailsServImpl.java
      /*******************/
      package com.rest.service;
      
      import java.util.List;
      import java.util.logging.Logger;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.core.ParameterizedTypeReference;
      import org.springframework.http.HttpMethod;
      import org.springframework.http.ResponseEntity;
      import org.springframework.stereotype.Service;
      import org.springframework.web.client.RestTemplate;
      
      import com.rest.entity.Details;
      @Service
      public class DetailsServImpl implements DetailsServ {
      
          
          private RestTemplate restTemplate;
      
          private String crmRestUrl;
              
          private Logger logger = Logger.getLogger(getClass().getName());
          
          @Autowired
          public DetailsServImpl(RestTemplate theRestTemplate, 
                  @Value(&quot;${crm.rest.url}&quot;) String theUrl) {
              restTemplate = theRestTemplate;
              crmRestUrl = theUrl;
                      
              logger.info(&quot;Loaded property:  crm.rest.url=&quot; + crmRestUrl);
          }
          
          
          @Override
          public List&lt;Details&gt; getDetails() {
              logger.info(&quot;in getDetails(): Calling REST API &quot; + crmRestUrl);
      
              // make REST call
              ResponseEntity&lt;List&lt;Details&gt;&gt; responseEntity = 
                                                  restTemplate.exchange(crmRestUrl, HttpMethod.GET, null, 
                                                                        new ParameterizedTypeReference&lt;List&lt;Details&gt;&gt;() {});
      
              // get the list of customers from response
              List&lt;Details&gt; theDetails = responseEntity.getBody();
      
              logger.info(&quot;in getDetails(): details&quot; + theDetails);
              return theDetails;
          }
      
          @Override
          public Details getDetails(int theId) {
              logger.info(&quot;in getDetails(): Calling REST API &quot; + crmRestUrl);
      
              // make REST call
              Details theDetails = 
                      restTemplate.getForObject(crmRestUrl + &quot;/&quot; + theId, 
                                                Details.class);
      
              logger.info(&quot;in saveDetails(): theDetails=&quot; + theDetails);
              
              return theDetails;
          }
      
          @Override
          public void saveDetails(Details theDetails) {
      logger.info(&quot;in saveSt(): Cudentalling REST API &quot; + crmRestUrl);
              
              int detailsId = theDetails.getId();
      
              // make REST call
              if (detailsId == 0) {
                  // add employee
                  restTemplate.postForEntity(crmRestUrl, theDetails, String.class);           
              
              } else {
                  // update employee
                  restTemplate.put(crmRestUrl, theDetails);
              }
      
              logger.info(&quot;in saveDetails(): success&quot;);
      
          }
      
          @Override
          public void deleteDetails(int theId) {
              logger.info(&quot;in deleteDetails(): Calling REST API &quot; + crmRestUrl);
      
              // make REST call
              restTemplate.delete(crmRestUrl + &quot;/&quot; + theId);
      
              logger.info(&quot;in deleteDetails(): deleted Details theId=&quot; + theId);
      
          }
      
      }
  </code></pre>
  <blockquote>
  <p>StudentService layer</p>
  </blockquote>
  <pre><code>StudentServImpl.java
  /***************************/
  package com.rest.service;
  import java.util.List;
  import java.util.logging.Logger;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.core.ParameterizedTypeReference;
  import org.springframework.http.HttpMethod;
  import org.springframework.http.ResponseEntity;
  import org.springframework.stereotype.Service;
  import org.springframework.web.client.RestTemplate;
  
  import com.rest.entity.Details;
  import com.rest.entity.Student;
  @Service
  public class StudentServImpl implements StudentServ {
      private RestTemplate restTemplate;
      private String crmRestUrl;
      private Logger logger = Logger.getLogger(getClass().getName());
      @Autowired
      public StudentServImpl(RestTemplate theRestTemplate,@Value(&quot;${crm.rest.url}&quot;) String theUrl){
          restTemplate = theRestTemplate;
          crmRestUrl = theUrl;
          logger.info(&quot;Loaded property:  crm.rest.url=&quot; + crmRestUrl);
      }
      @Override
      public List&lt;Student&gt; getStudent() {
          logger.info(&quot;in getCustomers(): Calling REST API &quot; + crmRestUrl);
  
          // make REST call
          ResponseEntity&lt;List&lt;Student&gt;&gt; responseEntity = 
                                              restTemplate.exchange(crmRestUrl, HttpMethod.GET, null, 
                                                                    new ParameterizedTypeReference&lt;List&lt;Student&gt;&gt;() {});
  
          // get the list of customers from response
          List&lt;Student&gt; theStudent = responseEntity.getBody();
          logger.info(&quot;in getCustomers(): customers&quot; + theStudent);
          return theStudent;
      }
      @Override
      public void saveStudent(Student theStudent) {
  logger.info(&quot;in saveSt(): Cudentalling REST API &quot; + crmRestUrl);    
  
          
          int studentId = theStudent.getId();
          // make REST call
          if (studentId == 0) {
              // add employee
              restTemplate.postForEntity(crmRestUrl, theStudent, String.class);           
          
          } else {
              // update employee
              restTemplate.put(crmRestUrl, theStudent);
          }
          logger.info(&quot;in saveStudent(): success&quot;);       
      }
      @Override
      public Student getStudent(int theId) {
          logger.info(&quot;in getCustomer(): Calling REST API &quot; + crmRestUrl);
          // make REST call
          Student theStudent = 
                  restTemplate.getForObject(crmRestUrl + &quot;/&quot; + theId, 
                                            Student.class);
          logger.info(&quot;in saveCustomer(): theCustomer=&quot; + theStudent);
          return theStudent;
      }
      @Override
      public void deleteStudent(int theId) {
          logger.info(&quot;in deleteStudent(): Calling REST API &quot; + crmRestUrl);
          // make REST call
          restTemplate.delete(crmRestUrl + &quot;/&quot; + theId);
          logger.info(&quot;in deleteStudent(): deleted Student theId=&quot; + theId);
      }
  }
      
  </code></pre>
  <p>-&gt;In this controller class when updating and deleting are not working.
  when we update it returns an empty row . and when we delete it throw an error. This the Single controller class for multiple entites of OneToOne relation.</p>
  <pre><code> ClientController.java
  /****************************/
  package com.rest.controller;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import com.rest.entity.Details;
  import com.rest.entity.Student;
  import com.rest.service.DetailsServ;
  import com.rest.service.StudentServ;
  
  @Controller
  @RequestMapping(&quot;/cont&quot;)
  public class ClientController {
  
      @Autowired
      private DetailsServ detailsServ;
      
      @Autowired
      private StudentServ studentServ;
      
      @GetMapping(&quot;/showForm&quot;)
      public String showForm(Model theModel){
          Details theDetails = new Details(); 
          Student theStudent = new Student();
          theDetails.setStudent(theStudent);
          theModel.addAttribute(&quot;for&quot;,theDetails);
          return &quot;the-for&quot;;
      }
      
      @PostMapping(&quot;/saveForm&quot;)
      private String saveForm(@ModelAttribute(&quot;for&quot;) Details theDetails) {
          detailsServ.saveDetails(theDetails);
          return &quot;redirect:/cont/list&quot;;
      }
      
      @GetMapping(&quot;/list&quot;)
      public String showList(Model theModel) {
          List&lt;Details&gt; theDetails = detailsServ.getDetails();
          theModel.addAttribute(&quot;details&quot;, theDetails);
          return &quot;list-table&quot;;
      }
      @GetMapping(&quot;/update&quot;)
      public String updateDetails(@RequestParam(&quot;detailsId&quot;) int theId, Model theModel) {
          Details theDetails =detailsServ.getDetails(theId);
          theModel.addAttribute(&quot;for&quot;,theDetails);
          return &quot;the-for&quot;;
      }
      @GetMapping(&quot;/delete&quot;)
      public String deleteDetails(@RequestParam(&quot;detailsId&quot;) int theId) {
          
          detailsServ.deleteDetails(theId);
          return &quot;redirect:/cont/list&quot;;
      }   
      
  </code></pre>
  <blockquote>
  <p>this one is the form to add or update details.</p>
  </blockquote>
  <pre><code> the-for.jsp
      /************/
      &lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
      
       &lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt; 
       &lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot;%&gt; 
       &lt;%@ page import=&quot;java.sql.*&quot;%&gt; 
      
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      
      &lt;head&gt;
          &lt;title&gt;Save Customer&lt;/title&gt;
      
          &lt;link type=&quot;text/css&quot;
                rel=&quot;stylesheet&quot;
                href=&quot;${pageContext.request.contextPath}/resources/css/style.css&quot;&gt;
      
          &lt;link type=&quot;text/css&quot;
                rel=&quot;stylesheet&quot;
                href=&quot;${pageContext.request.contextPath}/resources/css/add-customer-style.css&quot;&gt;
      &lt;/head&gt;
      
      &lt;body&gt;
          
          &lt;div id=&quot;wrapper&quot;&gt;
              &lt;div id=&quot;header&quot;&gt;
                  &lt;h2&gt;Form&lt;/h2&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      
          &lt;div id=&quot;container&quot;&gt;
              &lt;h3&gt;Save StudentDetails&lt;/h3&gt;
          
              &lt;form:form action=&quot;saveForm&quot; modelAttribute=&quot;for&quot; method=&quot;POST&quot;&gt;
      
                  &lt;!-- need to associate this data with customer id --&gt;
                  &lt;form:hidden path=&quot;id&quot; /&gt;
                          
                  &lt;table&gt;
                      &lt;tbody&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;label&gt;City name:&lt;/label&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:input path=&quot;city&quot; /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                      
                          &lt;tr&gt;
                              &lt;td&gt;&lt;label&gt;mobileno:&lt;/label&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:input path=&quot;mobileno&quot; /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
      
                          &lt;tr&gt;
                              &lt;td&gt;&lt;label&gt;name:&lt;/label&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:input path=&quot;student.name&quot; /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          
                          &lt;tr&gt;
                              &lt;td&gt;&lt;label&gt;class:&lt;/label&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;form:input path=&quot;student.clas&quot; /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
      
                          &lt;tr&gt;
                              &lt;td&gt;&lt;label&gt;&lt;/label&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;save&quot; /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
      
                      
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              
              
              &lt;/form:form&gt;
          
              &lt;div style=&quot;clear; both;&quot;&gt;&lt;/div&gt;
              
              &lt;p&gt;
                  &lt;a href=&quot;list&quot;&gt;Back to List&lt;/a&gt;
              &lt;/p&gt;
          
          &lt;/div&gt;
      
      &lt;/body&gt;
      
      &lt;/html&gt;
      
              
          }    
  </code></pre>
  <blockquote>
  <p>here this one is total CRUD page to display.</p>
  </blockquote>
  <pre><code> list-table.jsp
      /*****************/
      &lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt; 
       &lt;%@ page import = &quot;java.sql.*&quot; %&gt; 
      
       
      
      &lt;!DOCTYPE html&gt;
      
      &lt;html&gt;
      
      &lt;head&gt;
      &lt;title&gt;List&lt;/title&gt;
      
      &lt;!-- Required meta tags --&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
      
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link
          href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css&quot;
          rel=&quot;stylesheet&quot;
          integrity=&quot;sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1&quot;
          crossorigin=&quot;anonymous&quot;&gt;
      
      &lt;/head&gt;
      
      &lt;body&gt;
      
          &lt;div class=&quot;container&quot;&gt;
      
              &lt;h2 class=&quot;pt-2&quot;&gt;Student Manager&lt;/h2&gt;
      
              &lt;!-- put new button: Add Student --&gt;
      
              &lt;input type=&quot;button&quot; value=&quot;Add Student&quot;
                  onclick=&quot;window.location.href='showForm'; return false;&quot;
                  class=&quot;btn btn-primary btn-sm mb-3&quot; /&gt;
      
              &lt;!--  add our html table here --&gt;
      
              &lt;table class=&quot;table table-bordered table-striped&quot;&gt;
      
                  &lt;thead class=&quot;table-dark&quot;&gt;
                      &lt;tr&gt;
                          &lt;th&gt;city&lt;/th&gt;
                          &lt;th&gt;mobileno&lt;/th&gt;
                          &lt;th&gt;name&lt;/th&gt;
                          &lt;th&gt;class&lt;/th&gt;
                          &lt;th&gt;Action&lt;/th&gt; 
                      &lt;/tr&gt;
                  &lt;/thead&gt;
      
                  &lt;!-- loop over and print our students --&gt;
                  &lt;tbody&gt;
                      &lt;c:forEach var=&quot;tempDetails&quot; items=&quot;${details}&quot;&gt;
      
               &lt;!-- construct an &quot;update&quot; link with student id --&gt;
                          &lt;c:url var=&quot;updateLink&quot; value=&quot;/cont/update&quot;&gt; 
                              &lt;c:param name=&quot;detailsId&quot; value=&quot;${tempDetails.id}&quot; /&gt; 
                          &lt;/c:url&gt; 
      
                      &lt;!-- construct an &quot;delete&quot; link with student id --&gt;
                          &lt;c:url var=&quot;deleteLink&quot; value=&quot;/student/delete&quot;&gt; 
                              &lt;c:param name=&quot;studentId&quot; value=&quot;${tempStudent.id}&quot; /&gt; 
                          &lt;/c:url&gt; 
      
                          &lt;tr&gt;
                              
                              &lt;td&gt;${tempDetails.city}&lt;/td&gt;
                              &lt;td&gt;${tempDetails.mobileno}&lt;/td&gt;
                              &lt;td&gt;${tempDetails.student.name}&lt;/td&gt; 
                              &lt;td&gt;${tempDetails.student.clas}&lt;/td&gt; 
                              
                              &lt;td&gt;
                                  &lt;!-- display the update link --&gt; 
                                  &lt;a href=&quot;${updateLink}&quot;&gt;Update&lt;/a&gt; 
                                              |
                                   &lt;a href=&quot;${deleteLink}&quot; 
                                  onclick=&quot;if (!(confirm('Are you sure you want to delete this student?'))) return false&quot;&gt;Delete&lt;/a&gt;
                              &lt;/td&gt;
      
                          &lt;/tr&gt;
      
                      &lt;/c:forEach&gt;
                  &lt;/tbody&gt;
      
              &lt;/table&gt;
      
          &lt;/div&gt;
      
      &lt;/body&gt;
      
      &lt;/html&gt;
  </code></pre>