title: PATCH request in REST wth ReflectionUtils.findField using Spring getting IllegalArgumentException
body: <p>I'm learning and practicing CRUD Ops in which I'm trying to edit a MovieCatalogue using <strong>PATCH</strong> request but on hitting API endpoint I'm getting below ERROR!</p>
  <p>I've R&amp;D alot but didn't got appropriate solution</p>
  <p>Stacktrace</p>
  <pre><code>2022-03-09 14:17:48.110 ERROR 15483 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Can not set com.muhammadtaha.moviecatalogue.models.MovieData field com.muhammadtaha.moviecatalogue.models.MovieCatalogue.data to java.util.LinkedHashMap] with root cause
  
  java.lang.IllegalArgumentException: Can not set com.muhammadtaha.moviecatalogue.models.MovieData field com.muhammadtaha.moviecatalogue.models.MovieCatalogue.data to java.util.LinkedHashMap
      at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:na]
      at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:na]
      at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) ~[na:na]
      at java.base/java.lang.reflect.Field.set(Field.java:799) ~[na:na]
      at org.springframework.util.ReflectionUtils.setField(ReflectionUtils.java:637) ~[spring-core-5.3.16.jar:5.3.16]
      at com.muhammadtaha.moviecatalogue.services.implimentations.MovieCatalogueServiceImpl.lambda$editInfo$0(MovieCatalogueServiceImpl.java:70) ~[classes/:na]
      at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721) ~[na:na]
      at com.muhammadtaha.moviecatalogue.services.implimentations.MovieCatalogueServiceImpl.editInfo(MovieCatalogueServiceImpl.java:67) ~[classes/:na]
      at com.muhammadtaha.moviecatalogue.controllers.MovieCatalogueController.editInfo(MovieCatalogueController.java:47) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.16.jar:5.3.16]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:880) ~[spring-webmvc-5.3.16.jar:5.3.16]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.58.jar:4.0.FR]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.16.jar:5.3.16]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.16.jar:5.3.16]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.16.jar:5.3.16]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.16.jar:5.3.16]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.58.jar:9.0.58]
      at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
  </code></pre>
  <p>Postman response</p>
  <blockquote>
  <p>{
  &quot;timestamp&quot;: &quot;2022-03-09T09:17:48.123+00:00&quot;,
  &quot;status&quot;: 500,
  &quot;error&quot;: &quot;Internal Server Error&quot;,
  &quot;path&quot;: &quot;/movies/19&quot;
  }</p>
  </blockquote>
  <p>MovieCatalogueController.java</p>
  <pre><code>@PatchMapping
      public void editInfo(@RequestParam(name = &quot;id&quot;) Long id,
                           @RequestBody Map&lt;String, Object&gt; fields) {
          service.editInfo(id, fields);
          log.info(&quot;movie catalogue edited {}&quot;, fields);
      }
  </code></pre>
  <p>MovieCatalogueServiceImpl.java</p>
  <pre><code>@Override
      public void editInfo(Long id, Map&lt;String, Object&gt; fields) {
          if (repository.findById(id).isPresent() &amp;&amp; fields != null) {
              MovieCatalogue catalogue = new ObjectMapper().convertValue(fields, MovieCatalogue.class);
              fields.forEach((key, value) -&gt; {
                  Field field = ReflectionUtils.findField(MovieCatalogue.class, key);
                  field.setAccessible(true);
                  ReflectionUtils.setField(field, catalogue, value);
              });
              repository.save(catalogue);
          }
      }
  </code></pre>
  <p>MovieCatalogue.java</p>
  <pre><code>@Entity
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  @Builder
  @EqualsAndHashCode(callSuper = false)
  public class MovieCatalogue extends BaseEntity&lt;MovieCatalogue&gt; {
  
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;data_id&quot;, referencedColumnName = &quot;id&quot;)
      private MovieData data;
  
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;rating_id&quot;, referencedColumnName = &quot;id&quot;)
      private Rating rating;
  }
  </code></pre>
  <p>MovieData.java</p>
  <pre><code>@Entity
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  @Builder
  @EqualsAndHashCode(callSuper = false)
  public class MovieData extends BaseEntity&lt;MovieData&gt; {
  
      private String name;
      private String description;
  
      @OneToOne(mappedBy = &quot;data&quot;)
      @JsonIgnore
      @Transient
      private MovieCatalogue movieCatalogue;
  }
  </code></pre>
  <p>Rating.java</p>
  <pre><code>@Entity
  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  @Builder
  @EqualsAndHashCode(callSuper = false)
  public class Rating extends BaseEntity&lt;Rating&gt; {
  
      private Double rating;
  
      @OneToOne(mappedBy = &quot;rating&quot;)
      @JsonIgnore
      @Transient
      private MovieCatalogue movieCatalogue;
  }
  </code></pre>
  <p>PATCH API endpoint is</p>
  <blockquote>
  <p>http://localhost:8080/movies/{id}</p>
  </blockquote>
  <pre><code>// for data persistance in Postgres
  @Repository
  public interface MovieCatalogueRepository extends JpaRepository&lt;MovieCatalogue, Long&gt; {}
  </code></pre>