title: Cannot persist object because of foreign key relationship java.lang.IllegalArgumentException: object is not an instance of declaring class
body: <p>I have two entities A and B - A is auto-generated by using swagger-codegen-maven-plugin and B is created by me and contains a Many To One relationship to A. The auto-generated one (A) has 4 attributes, but the table in the database which corresponds to it has an additional column called id, which is the reference column for the foreign key in the second entity(B), which is called idA.</p>
  
  <p>I have to persist objects of type B in the database and I need to set the value for idA as well. Even though the value is well set, by getting it from an object of type entity A ( taken from the database ), I get java.lang.IllegalArgumentException: object is not an instance of declaring class. I get the value if I have an object of type A, but if I assign it to object B, it gives me that error.</p>
  
  <p>I will show the code:</p>
  
  <pre><code>@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2019-10-31T17:01:04.704+02:00")
  
  public class A{
  
    protected Integer accountId = null;
    protected Integer range = null;
    protected Integer address = null;
    protected Integer agency = null;
    protected Long id;
  
  @Column(
          name = "ID"
      )
      @GeneratedValue(
          strategy = GenerationType.AUTO
      )
      public Long getId() {
          return this.id;
      }
  
      public void setid(Long value) {
          this.id = value;
      }
  
  </code></pre>
  
  <pre><code>
  @Entity
  @Table(name = "STUDENTS")
  public class B implements Serializable {
  
          private long id;
          private String name;
          private long idA;
          private long age;
  
  @ManyToOne(targetEntity = A.class, cascade = {
                  CascadeType.ALL
          })
          @JoinColumn(name = "ID_A", referencedColumnName = "ID", nullable = false)
          public long getIdA() {
              return idA;
          }
  
          public void setIdA(long idA) {
              this.idA= idA;
          }
  
  </code></pre>
  
  <pre><code>
  EntityManager em;
  
  List&lt;A&gt; entitiesList = repositoryA.findAllEntities(); // method which gets all the entries from the table
  
  for(int i = 0; i &lt; entitiesList.size(); i++) {
    B target = new B();
    target.setName("Andrew");
    target.setidA(entitiesList.get(i).getId()); // the value for the id is correctly taken from the database entry
    em.persist(B); ---&gt; line which gives error because of IllegalArgumentException
  
  </code></pre>
  
  <p>How can I persist my object if the auto-generated class does not have a getter or setter for the column which is the foreign key ?</p>