title: Error accessing field [private int ...] by reflection for persistent property
body: <p>I keep getting the Error message in the File, I do not understand how to fix it and was unable to find a solution.</p>
  <p><strong>This is my model.</strong></p>
  <pre class="lang-java prettyprint-override"><code>package bbw.ch.lf.model;
  
  import org.springframework.context.annotation.EnableMBeanExport;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.GetMapping;
  
  import javax.persistence.*;
  import java.util.Calendar;
  import java.util.Date;
  
  
  @Entity
  @NamedQuery(name=&quot;Day.findAll&quot;, query = &quot;SELECT d FROM Day d&quot;)
  @Table(name=&quot;day&quot;)
  public class Day {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;id&quot;, unique = true)
      private int id;
  
      @Column(name = &quot;date&quot;)
      private String date;
  
      @Column(name = &quot;year&quot;)
      private String year;
  
      @Column(name = &quot;entry&quot;)
      private String entry;
  
      public Day() {};
  
      public Day(String entry, String date) {
          this.date = date;
          this.entry = entry;
  
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getDate() {
          return date;
      }
  
      public void setDate(String date) {
          this.date = date;
      }
  
      public String getYear() {
          return year;
      }
  
      public void setYear(String year) {
          this.year = year;
      }
  
      public String getEntry() {
          return entry;
      }
  
      public void setEntry(String entry) {
          this.entry = entry;
      }
  }
  
  </code></pre>
  <p><strong>In my DayController, I have this</strong></p>
  <pre><code>//Create Day
      @GetMapping(&quot;day/create-day&quot;)
      public String redirectToCreateDay(Model model){
          model.addAttribute(&quot;day&quot;, new Day());
          return &quot;addDay&quot;;
      }
  
      @PostMapping(&quot;day/create&quot;)
      public String createDay(@ModelAttribute Day day, Model model) {
          dayRepo.createDay(day);
          model.addAttribute(&quot;day&quot;, dayRepo.fetchAllDays());
          return &quot;days&quot;;
      }
  </code></pre>
  <p><strong>And my DayRepository has this</strong></p>
  <pre class="lang-java prettyprint-override"><code> EntityManagerFactory emFactory;
   EntityManager em;
  
      public void setup() {
          emFactory = Persistence.createEntityManagerFactory(&quot;MyPersistenceUnit&quot;);
          em = emFactory.createEntityManager();
  
      }
  
      public DayRepository() {
          setup();
      }
  
  public void createDay(Day day) {
          try {
              em.getTransaction().begin();
              em.persist(day);
              em.getTransaction().commit();
          }catch (Exception e) {
              e.printStackTrace();
              em.getTransaction().rollback();
          }
      }
  </code></pre>
  <p>Here is the full Error Message</p>
  <blockquote>
  <p>javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int bbw.ch.lf.model.Day.id] by reflection for persistent property [bbw.ch.lf.model.Day#id] : bbw.ch.lf.model.Day@60d5b12
  at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
  at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
  at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
  at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:810)
  at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:788)
  at bbw.ch.lf.Repository.DayRepository.createDay(DayRepository.java:53)
  at bbw.ch.lf.controller.DayController.createDay(DayController.java:46)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.base/java.lang.reflect.Method.invoke(Method.java:567)
  at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:807)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
  at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
  at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
  at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.base/java.lang.Thread.run(Thread.java:835)
  Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int bbw.ch.lf.model.Day.id] by reflection for persistent property [bbw.ch.lf.model.Day#id] : bbw.ch.lf.model.Day@60d5b12
  at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
  at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
  at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:5003)
  at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4703)
  at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
  at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:556)
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62)
  at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:803)
  ... 57 more
  Caused by: java.lang.IllegalArgumentException: Can not set int field bbw.ch.lf.model.Day.id to bbw.ch.lf.model.Day
  at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
  at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
  at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
  at java.base/jdk.internal.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
  at java.base/java.lang.reflect.Field.getInt(Field.java:594)
  at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62)
  ... 65 more</p>
  </blockquote>