title: Hibernate - Object Is Not An Instance Of Declaring Class
body: <p>I have a very simple entity:</p>
  
  <pre><code>import javax.persistence.*;
  
  @Entity(name = "Message")
  public class MessageEntity {
  
      private Long id;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      public Long getId() {
          return this.id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>The <em>persistence.xml</em> contains <code>hibernate.hbm2ddl.auto=create-drop</code>, so the tables are created correctly based on the annotations. Still, when I persist the entity, I get the following exception:</p>
  
  <pre><code>org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of org.acme.project.MessageEntity.id
  at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:192) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:116) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
  ... 154 more
  Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_20]
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_20]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_20]
  at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_20]
  at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  ... 165 more
  </code></pre>
  
  <p>Or when field is annotated instead of the getter:</p>
  
  <pre><code>org.hibernate.PropertyAccessException: could not get a field value by reflection getter of org.acme.project.MessageEntity.id
  at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:60) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:116) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
  ... 154 more
  Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field org.acme.project.MessageEntity.id to org.acme.project.MessageEntity
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) [rt.jar:1.8.0_20]
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) [rt.jar:1.8.0_20]
  at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) [rt.jar:1.8.0_20]
  at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) [rt.jar:1.8.0_20]
  at java.lang.reflect.Field.get(Field.java:387) [rt.jar:1.8.0_20]
  at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
  ... 165 more
  </code></pre>
  
  <p>The code producing the exceptions is very complicated, so take a moment to really get it:</p>
  
  <pre><code>@PersistenceContext
  private EntityManager em;
  
  public void doMagic() {
      this.em.persist(new MessageEntity());
  }
  </code></pre>
  
  <p>It's probably a small and very stupid mistake that makes Hibernate think it should set the ID to <code>MessageEntity</code>, but I can't seem to find where it is. What's the problem?</p>