title: Why this Error java.lang.IllegalArgumentException: object is not an instance of declaring class is shown in the code
body: <p><strong>Problem:</strong></p>
  <p><strong>GivenProblemStatement</strong>:</p>
  <p>1)Define en Entity Teacher.A Teacher can teach many Subjects.</p>
  <p>2)Subject have another Subject as Parent Subject.</p>
  <p>3)Teacher uses a Composite key(name,subject).</p>
  <p><strong>Solution:</strong></p>
  <p>I have implemented the above points and I am trying to save the details
  in the database using HibernateTemplate of Teacher class Object as well as parent class object.
  <strong>But</strong>,I am not able to save the parent class object.
  <strong>Reason:</strong>
  Because of the error shown below</p>
  <hr />
  <p><strong>Error:</strong></p>
  <pre><code>Exception in thread &quot;main&quot; org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of str.Parent.subjectId; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of str.Parent.subjectId
  at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
  at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
  at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
  at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
  at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
  at dao.EmployeeDao.saveEmployee(EmployeeDao.java:23)
  at str.A.abc(A.java:26)
  at str.OurLogic.main(OurLogic.java:56)
  Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of str.Parent.subjectId
  at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:198)
  at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:227)
  at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3850)
  at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3558)
  at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
  at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
  at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:120)
  at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
  at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
  at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
  at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
  at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
  at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
  at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
  at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
  at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
  ... 5 more
  Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
  ... 23 more
  </code></pre>
  <hr />
  <h1>Code</h1>
  <p><strong>OurLogic.java</strong></p>
  <pre><code>Below is my code:
  package str;
  public class OurLogic {
  
  public static void main(String args[])
  {
  Teacher t =new Teacher();
  t.setTeacherId(101);
  t.setTeacherName(&quot;Jalaj&quot;);
  
  //creating 3 child objects
  Subject s1=new Subject();
  
  s1.setSubjectId(504);
  s1.setSubjectName(&quot;JSE&quot;);
  
  Subject s2=new Subject();
  
  s2.setSubjectId(505);
  s2.setSubjectName(&quot;JEE&quot;);
  
  Subject s3=new Subject();
  
  s3.setSubjectId(506);
  s3.setSubjectName(&quot;Spring&quot;);
  Parent p=new Parent();
  p.setParentSubjectName(&quot;Java&quot;); 
  p.setS(s1);
  // adding child objects to set, as we taken 3rd property set in parent
  Set s=new HashSet();
  
  s.add(s1);
  s.add(s2);
  s.add(s3);
  
  t.setSubjects(s);
  
  String subjectName=&quot;JEE&quot;;//Given a subjectN
  A.abc(t,p,subjectName,s);
  }
  </code></pre>
  <hr />
  <p><strong>A.java</strong></p>
  <pre><code>package str;
  public class A {  
  public static void abc(Teacher t,Parent p,String s1,Set s) {  
  ApplicationContext bfact=new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);
  EmployeeDao dao=(EmployeeDao)bfact.getBean(&quot;d&quot;);  
        
      
  //For insert
  dao.saveEmployee(t);  
  dao.saveEmployee(p);
  }
  </code></pre>
  <hr />
  <p><strong>EmployeeDao</strong></p>
  <pre><code>package dao;
  public class EmployeeDao {  
  HibernateTemplate template;  
  public void setTemplate(HibernateTemplate template) {  
  this.template = template;  
  }  
  //method to save employee  
  public void saveEmployee(Teacher t){  
  template.save(t);  
  }  
  public void saveEmployee(Parent p){  
  template.save(p);  
  } 
  </code></pre>
  <hr />
  <p><strong>Parent.java</strong></p>
  <pre><code>public class Parent { 
  private int subjectId;
  private String parentSubjectName;
  private Subject s;
  public Subject getS() {
      return s;
  }
  public void setS(Subject s) {
      this.s = s;
  }
  public int getSubjectId() {
      return subjectId;
  }
  public void setSubjectId(int subjectId) {
      this.subjectId = subjectId;
  }
  public String getParentSubjectName() {
      return parentSubjectName;
  }
  public void setParentSubjectName(String parentSubjectName) {
      this.parentSubjectName = parentSubjectName;
  }   
  }
  </code></pre>
  <hr />
  <p><strong>Subject.java</strong></p>
  <pre><code>public class Subject implements Serializable{
  private int subjectId;
  private String subjectName;
  private int forevenId;
  public int getSubjectId() {
  return subjectId;
  }
  public void setSubjectId(int subjectId) {
  this.subjectId = subjectId;
  }
  public String getSubjectName() {
  return subjectName;
  }
  public void setSubjectName(String subjectName) {
  this.subjectName = subjectName;
  }
  public int getForevenId() {
  return forevenId;
  }
  public void setForevenId(int forevenId) {
  this.forevenId = forevenId;
  }
  }
  </code></pre>
  <hr />
  <h2>Teacher.java</h2>
  <pre><code>public class Teacher implements Serializable{
  private int teacherId;
  private String teacherName;
  private Set subjects;
  public int getTeacherId() {
  return teacherId;
  }
  public void setTeacherId(int teacherId) {
  this.teacherId = teacherId;
  }
  public String getTeacherName() {
  return teacherName;
  }
  public void setTeacherName(String teacherName) {
  this.teacherName = teacherName;
  }
  public Set getSubjects() {
  return subjects;
  }
  public void setSubjects(Set subjects) {
  this.subjects = subjects;
  } 
  }
  </code></pre>
  <hr />
  <p><strong>applicationContext.xml</strong></p>
  <pre><code>&lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;&gt;  
  &lt;property name=&quot;driverClassName&quot;  value=&quot;com.mysql.jdbc.Driver&quot;&gt;  &lt;/property&gt;  
  &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql://localhost:3306/JalajSchema&quot;&gt; &lt;/property&gt;  
  &lt;property name=&quot;username&quot; value=&quot;root&quot;&gt;&lt;/property&gt;  
  &lt;property name=&quot;password&quot; value=&quot;&quot;&gt;&lt;/property&gt;  
  &lt;/bean&gt;     
  &lt;bean id=&quot;mysessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&gt;  
  &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;/property&gt;          
  &lt;property name=&quot;mappingResources&quot;&gt;  
  &lt;list&gt;  
  &lt;value&gt;Teacher.hbm.xml&lt;/value&gt;           
  &lt;value&gt;Subject.hbm.xml&lt;/value&gt;   
  &lt;value&gt;Parent.hbm.xml&lt;/value&gt;  
  &lt;/list&gt;  
  &lt;/property&gt;            
  &lt;property name=&quot;hibernateProperties&quot;&gt;  
  &lt;props&gt;                &lt;propkey=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;  
  &lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;create&lt;/prop&gt;  
  &lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/prop&gt;          
  &lt;/props&gt;  
  &lt;/property&gt;  
  &lt;/bean&gt;      
  &lt;bean id=&quot;template&quot; class=&quot;org.springframework.orm.hibernate3.HibernateTemplate&quot;&gt;  
  &lt;property name=&quot;sessionFactory&quot; ref=&quot;mysessionFactory&quot;&gt;&lt;/property&gt;  
  &lt;/bean&gt;       
  &lt;bean id=&quot;d&quot; class=&quot;dao.EmployeeDao&quot;&gt;  
  &lt;property name=&quot;template&quot; ref=&quot;template&quot;&gt;&lt;/property&gt;  
  &lt;/bean&gt;       
  &lt;/beans&gt;  
  </code></pre>
  <hr />
  <p><strong>Subject.hbm.xml</strong></p>
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name=&quot;str.Subject&quot; table=&quot;subject&quot;&gt; 
  &lt;id name=&quot;subjectId&quot; column=&quot;subjid&quot;  /&gt;
  &lt;property name=&quot;subjectName&quot; column=&quot;subjname&quot; length=&quot;10&quot;/&gt;
  &lt;property name=&quot;forevenId&quot; column=&quot;forevenid&quot;  insert=&quot;false&quot; /&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  <hr />
  <p><strong>Teacher.hbm.xml</strong></p>
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name=&quot;str.Teacher&quot; table=&quot;Teacher1&quot;&gt;
  &lt;id name=&quot;teacherId&quot; column=&quot;teachid&quot;  /&gt; 
  &lt;property name=&quot;teacherName&quot; column=&quot;tname&quot; length=&quot;10&quot;/&gt;
  &lt;set name=&quot;subjects&quot; cascade=&quot;all&quot; &gt;
  &lt;key column=&quot;forevenid&quot; /&gt;
  &lt;one-to-many class=&quot;str.Subject&quot; /&gt;
  &lt;/set&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  <hr />
  <p><strong>Parent.hbm.xml</strong></p>
  <pre><code>&lt;hibernate-mapping&gt;
  &lt;class name=&quot;str.Parent&quot; table=&quot;Parent&quot;&gt;
  &lt;id name=&quot;subjectId&quot; column=&quot;subjectId&quot; &gt;
  &lt;generator class=&quot;foreign&quot;&gt;  
  &lt;param name=&quot;property&quot;&gt;s&lt;/param&gt;
  &lt;/generator&gt;  
  &lt;/id&gt;  
  &lt;property name=&quot;parentSubjectName&quot; column=&quot;parentSubjectName&quot; length=&quot;10&quot;/&gt;
  &lt;one-to-one name=&quot;s&quot; class=&quot;str.Parent&quot; cascade=&quot;all&quot; /&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  <blockquote>
  <p>Can anyone guide me how to correct it?</p>
  </blockquote>